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

问题描述

我已经用过:
user-secret set "AWSAccessKey" "my access key"
user-secret set ""AWSSecretKey" "my secret key"

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

to get my keys into the secret store.

我有这门课,我将用它来通过 Amazon 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);
        }
    }
}

为了在 MailHelper 类中使用这些密钥,我在 Startup.cs 中放了什么?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"
  }
}

在 Startup 中,你需要这个:

in Startup you would need this:

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 只是另一个配置源,因此您可以将设置放在那里而不是 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"

这相当于我为 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) 用户机密注入我自己的实用程序类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 18:13