Reputation: 1561
I can see that the secrets get pushed into some kind of storage for the configuration, but I don't understand how to access the variable.
During config building, I can see that it read my app secret, but later when I inject IConfiguration
into an App Service, the key is not there.
Here is what I have so far:
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("[email protected]", "Example User");
var subject = "Sending with SendGrid is Fun";
var to = new EmailAddress("[email protected]", "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);
}
Some documentation about how to do this would be nice.
Upvotes: 0
Views: 749
Reputation: 1561
The way that worked for me in the end is using this guide: https://www.twilio.com/blog/2018/05/user-secrets-in-a-net-core-web-app.html
example secrets.json:
{
"Authentication": {
"Google": {
"ClientId": "baerbaeb.apps.googleusercontent.com",
"ClientSecret": "bahababbtY8OO"
},
"Microsoft": {
"ApplicationId": "barberbaerbaerbaerb",
"Password": "aerbaerbaerbaerbaerb"
}
},
"Sendgrid": {
"ApiKey": "aerbaerbaerbearbearbaerbeabeabr"
}
}
Azure Application Settings would be formatted like this:
Authentication:Google:ClientId
Sendgrid:ApiKey
Add some config classes.
public class GoogleApiConfig
{
public string ClientId { get; set; }
public string ClientSecret { get; set; }
}
Then register them like this:
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"));
Then just inject IOptions<MicrosoftApiConfig>
and use as normal - simple and strong!
Upvotes: 0
Reputation: 43098
Injecting IConfiguration
won't get you the user secrets.
Use the static method AppConfigurations.Get
and specify addUserSecrets: true
instead:
public class EmailAppService : MyAppServiceBase, IEmailAppService
{
private readonly IConfigurationRoot _appConfiguration;
public EmailAppService()
{
_appConfiguration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder(), addUserSecrets: true);
}
// ...
}
In the Web projects, you can inject IHostingEnvironment
and use the extension method:
_appConfiguration = env.GetAppConfiguration();
Upvotes: 1