簡體   English   中英

如何使用ABP讀取UserSecrets?

[英]How do I read UserSecrets using ABP?

我可以看到這些秘密已被推送到某種類型的存儲中,用於配置,但是我不知道如何訪問變量。 在配置構建過程中,我可以看到它讀取了我的應用程序密碼,但是稍后當我將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);
    }

一些有關如何執行此操作的文檔會很好。

注入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();

最終為我工作的方法是使用此指南: https : //www.twilio.com/blog/2018/05/user-secrets-in-a-net-core-web-app.html

示例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 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>並正常使用-簡單而強大!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM