代码之家  ›  专栏  ›  技术社区  ›  Worthy7

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

  •  0
  • Worthy7  · 技术社区  · 8 年前

    我可以看到这些秘密被推送到了某种配置存储中,但我不知道如何访问变量。 在配置构建期间,我可以看到它读取了我的应用程序机密,但稍后当我注入 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);
        }
    

    一些关于如何做到这一点的文档会很好。

    2 回复  |  直到 8 年前
        1
  •  1
  •   aaron    8 年前

    注入 IConfiguration 不会让你知道用户的秘密。

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

    public class EmailAppService : MyAppServiceBase, IEmailAppService
    {
        private readonly IConfigurationRoot _appConfiguration;
    
        public EmailAppService()
        {
            _appConfiguration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder(), addUserSecrets: true);
        }
    
        // ...
    }
    

    在Web项目中,可以注入 IHostingEnvironment 并使用扩展方法:

    _appConfiguration = env.GetAppConfiguration();
    
        2
  •  0
  •   Worthy7    8 年前

    最终对我有效的方法是使用此指南: https://www.twilio.com/blog/2018/05/user-secrets-in-a-net-core-web-app.html

    示例机密。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 class GoogleApiConfig
    {
        public string ClientId { get; set; }
        public string ClientSecret { get; set; }
    }
    

    然后按如下方式注册:

    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"));
    

    然后注射 IOptions<MicrosoftApiConfig> 并像平常一样使用-简单而强大!