我已经使用过:
user-secret set "AWSAccessKey" "my access key"
和
user-secret set ""AWSSecretKey" "my secret key"
让钥匙进入秘密商店。
我已经通过亚马逊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);
}
}
}
为了在MailHelper类中获取这些密钥,我在Startup.cs中放了什么?什么需要添加到MailHelper类本身?
我已经看过MVC控制器的一些示例,但我找不到任何尚未实现某些预期接口的自定义实用程序类。
答案 0 :(得分:13)
您的设置类:
public class AwsSettings
{
public string AccessKey { get; set; } = string.Empty;
public string AccessSecret{ get; set; } = string.Empty;
}
如果您的设置在config.json中,您可以这样做:
{
"AwsSettings": {
"AccessKey": "yourkey",
"AccessSecret": "yoursecret"
}
}
在启动时你需要这个:
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
显示的结构现在,设置将从用户机密中加载并注入您的邮件程序