C# 如何使用ABP读取用户机密?

C# 如何使用ABP读取用户机密?,c#,configuration,aspnetboilerplate,C#,Configuration,Aspnetboilerplate,我可以看到这些秘密被推送到某种配置存储中,但我不知道如何访问变量。 在配置构建期间,我可以看到它读取了我的应用程序机密,但后来当我将IConfiguration注入应用程序服务时,密钥不在那里 以下是我到目前为止的情况: public class EmailAppService : MyAppServiceBase, IEmailAppService { private IConfiguration _configuration { get; } public EmailApp

我可以看到这些秘密被推送到某种配置存储中,但我不知道如何访问变量。 在配置构建期间,我可以看到它读取了我的应用程序机密,但后来当我将
IConfiguration
注入应用程序服务时,密钥不在那里

以下是我到目前为止的情况:

public class EmailAppService : MyAppServiceBase, IEmailAppService
{
    private IConfiguration _configuration { get; }

    public EmailAppService(IConfiguration Configuration)
    {
        _configuration = Configuration;
    }

    /// <summary>
    /// Signup just involved sending an email to Rhyse at the moment.
    /// </summary>
    /// <param name="input">Users email</param>
    [AbpAllowAnonymous]
    public async Task<Response> SignupToBetaAsync(SignUpToBetaInput input)
    {
        // from https://sendgrid.com/docs/Integrate/Code_Examples/v3_Mail/csharp.html
        var apiKey = _appConfiguration["SENDGRID_API_KEY"];
        var client = new SendGridClient(apiKey);
        var from = new EmailAddress("test@example.com", "Example User");
        var subject = "Sending with SendGrid is Fun";
        var to = new EmailAddress("fakeemail@gmail.com", "Example User");
        var plainTextContent = "and easy to do anywhere, even with C#";
        var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
        var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
        return await client.SendEmailAsync(msg);
    }
公共类EmailAppService:MyAppServiceBase,IEmailAppService
{
专用IConfiguration\u配置{get;}
公共EmailAppService(IConfiguration配置)
{
_配置=配置;
}
/// 
///目前注册只涉及向Rhyse发送电子邮件。
/// 
///用户电子邮件
[AbpAllowAnonymous]
公共异步任务SignupToBetaAsync(SignUpToBetaInput输入)
{
//从https://sendgrid.com/docs/Integrate/Code_Examples/v3_Mail/csharp.html
var apiKey=_appConfiguration[“SENDGRID_API_KEY”];
var client=新的SendGridClient(apiKey);
var from=新电子邮件地址(“test@example.com“,”示例用户“);
var subject=“使用SendGrid发送很有趣”;
var to=新的电子邮件地址(“fakeemail@gmail.com“,”示例用户“);
var plainTextContent=“而且在任何地方都可以轻松完成,即使使用C#”;
var htmlContent=“并且在任何地方都可以轻松完成,即使使用C#”;
var msg=MailHelper.CreateSingleEmail(发件人、收件人、主题、明文内容、htmlContent);
返回wait client.sendmailasync(msg);
}

一些关于如何实现这一点的文档会很好。

注入
IConfiguration
不会为您获取用户机密

使用静态方法
AppConfigurations.Get
并指定
addUserSecrets:true

公共类EmailAppService:MyAppServiceBase,IEmailAppService
{
专用只读IConfigurationRoot\u appConfiguration;
公共电子邮件应用服务()
{
_appConfiguration=AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder(),addUserSecrets:true);
}
// ...
}
在Web项目中,您可以注入
IHostingEnvironment
并使用扩展方法:

\u appConfiguration=env.GetAppConfiguration();

最终对我有效的方法是使用本指南:

示例secrets.json:

{
  "Authentication": {
    "Google": {
      "ClientId": "baerbaeb.apps.googleusercontent.com",
      "ClientSecret": "bahababbtY8OO"
    },
    "Microsoft": {
      "ApplicationId": "barberbaerbaerbaerb",
      "Password": "aerbaerbaerbaerbaerb"
    }
  },
  "Sendgrid": {
    "ApiKey": "aerbaerbaerbearbearbaerbeabeabr"
  }
}
Azure应用程序设置的格式如下:

Authentication:Google:ClientId
Sendgrid:ApiKey
public IServiceProvider ConfigureServices(IServiceCollection services)
{
  services.Configure<GoogleApiConfig>_appConfiguration.GetSection("Authentication:Google"));
    services.Configure<MicrosoftApiConfig>_appConfiguration.GetSection("Authentication:Microsoft"));
  services.Configure<SendgridApiConfig>_appConfiguration.GetSection("Sendgrid"));
添加一些配置类

public class GoogleApiConfig
{
    public string ClientId { get; set; }
    public string ClientSecret { get; set; }
}
然后像这样注册它们:

Authentication:Google:ClientId
Sendgrid:ApiKey
public IServiceProvider ConfigureServices(IServiceCollection services)
{
  services.Configure<GoogleApiConfig>_appConfiguration.GetSection("Authentication:Google"));
    services.Configure<MicrosoftApiConfig>_appConfiguration.GetSection("Authentication:Microsoft"));
  services.Configure<SendgridApiConfig>_appConfiguration.GetSection("Sendgrid"));
公共IServiceProvider配置服务(IServiceCollection服务)
{
services.Configure_appConfiguration.GetSection(“身份验证:Google”);
services.Configure_appConfiguration.GetSection(“身份验证:Microsoft”);
services.Configure_appConfiguration.GetSection(“Sendgrid”);

然后只需注入
IOptions
并正常使用-简单而强大!

@aaron事实证明此解决方案在部署到Azure时不起作用。我得到的错误与此github问题中的错误相同