本文介绍了我如何注入ASP.NET 5(vNext)用户秘密到我自己的工具类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用:结果
    用户组机密AWSAccessKey,我的访问密钥和结果
    用户组机密,AWSSecretKey,我的秘密钥匙

I have already used:
user-secret set "AWSAccessKey" "my access key" and
user-secret set ""AWSSecretKey" "my secret key"

让我的钥匙进入秘密商店。

to get my keys into the secret store.

我有了这个类,我将使用通过亚马逊SES发送电子邮件,但我不知道如何让我的钥匙进去。

I've got this class that I will be using to send email via Amazon SES, but I don't know how to get my keys into it.

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类本身?

What do I put in my Startup.cs in order to get those keys available in my MailHelper class? What needs to be added to the MailHelper class itself?

我见过MVC控制器的一些例子,但我无法找到的,尚未实施一些预期的接口的自定义工具类东西。

I've seen some examples for MVC Controllers, but I couldn't find anything for custom utility classes that are not already implementing some expected interface.

推荐答案

一个为你设置类:

public class AwsSettings
{
    public string AccessKey { get; set; } = string.Empty;
    public string AccessSecret{ get; set; } = string.Empty;
}

如果您的设置是在config.json你会做这样的:

if your settings were in config.json you would do it like this:

{
  "AwsSettings": {
    "AccessKey": "yourkey",
    "AccessSecret": "yoursecret"
  }
}

在启动你需要这样的:

services.Configure<AwsSettings>(Configuration.GetSection("AwsSettings"));

您的邮件类将需要注入构造函数中设置

your mailer class would need the settings injected into constructor

using Microsoft.Framework.OptionsModel;

public class MailHelper
{
    public MailHelper(IOptions<AwsSettings> awsOptionsAccessor)
    {
        awsSettings = awsOptionsAccessor.Options;
    }
    private AwsSettings awsSettings;

    ... your other methods

}

UserSecrets只是另一个configuation源这样反而config.json你可以把设置有,但你需要模拟结构,因此它可以映射到您的类,所以你将它们像这样用冒号:

UserSecrets is just another configuation source so instead of config.json you can put the settings there but you need to simulate the structure so it can map to your class so you set them like this with colons:

user-secret set "AwsSettings:AccessKey" "my access key" and
user-secret set "AwsSettings:AccessSecret" "my secret key"

这等同于结构I表现出对config.json

that is equivalent to the structure I showed for config.json

现在的设置将从用户机密被加载并注​​入到你的邮件

now the settings will be loaded from user secrets and injected into your mailer

这篇关于我如何注入ASP.NET 5(vNext)用户秘密到我自己的工具类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 04:59