我已经用过了:
user-secret设置“AWSAccessKey”“我的访问密钥”和
用户密码设置“”AWSSecretKey“”我的密钥“
user-secret设置“AWSAccessKey”“我的访问密钥”和
用户密码设置“”AWSSecretKey“”我的密钥“
让我的钥匙进入秘密商店.
我有这个课程,我将用它来通过Amazon SES发送电子邮件,但我不知道如何将我的密钥插入其中.
public class MailHelper { public void SendEmailSes(string senderAddress,string receiverAddress,string subject,string body) { using (var client = new AmazonSimpleEmailServiceClient( new BasicAWSCredentials("[AWSAccessKey]","[AWSSecretKey]"),RegionEndpoint.USEast1)) { var sendRequest = new SendEmailRequest { Source = senderAddress,Destination = new Destination { ToAddresses = new List<string> { receiverAddress } },Message = new Message { Subject = new Content(subject),Body = new Body { Text = new Content(body) } } }; var response = client.SendEmail(sendRequest); } } }
我在Startup.cs中放了什么才能在MailHelper类中获取这些键?需要添加什么到MailHelper类本身?
我已经看到了MVC控制器的一些示例,但我找不到任何尚未实现某些预期接口的自定义实用程序类.
解决方法
适合您的设置的课程:
public class AwsSettings { public string AccessKey { get; set; } = string.Empty; public string AccessSecret{ get; set; } = string.Empty; }
如果您的设置在config.json中,您可以这样做:
{ "AwsSettings": { "AccessKey": "yourkey","AccessSecret": "yoursecret" } }
在Startup中你需要这个:
services.Configure<AwsSettings>(Configuration.GetSection("AwsSettings"));
using Microsoft.Framework.OptionsModel; public class MailHelper { public MailHelper(IOptions<AwsSettings> awsOptionsAccessor) { awsSettings = awsOptionsAccessor.Options; } private AwsSettings awsSettings; ... your other methods }
UserSecrets只是另一个配置源代码而不是config.json你可以把设置放在那里,但是你需要模拟结构,这样它就可以映射到你的类,所以你用冒号设置它们:
user-secret set "AwsSettings:AccessKey" "my access key" and user-secret set "AwsSettings:AccessSecret" "my secret key"
这相当于我为config.json展示的结构