我如何使用 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>
并正常使用 - 简单而强大!
我可以看到机密被推送到某种配置存储中,但我不明白如何访问变量。
在配置构建过程中,我可以看到它读取了我的应用程序机密,但稍后当我将 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>
并正常使用 - 简单而强大!