How to set SendGrid API Key in the code
I have followed the instruction but I do not know how to set the SendGrid API Key. You suggest "The Options pattern is used to access the user account and key settings. For more information, see the configuration." But those links provide general information. Could you please add to the example
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: fe1bea60-d173-f6d2-3559-b147699e295a
- Version Independent ID: 98e7aacb-2a01-d581-9b03-bfef2893163e
- Content: Account confirmation and password recovery in ASP.NET Core
- Content Source: aspnetcore/security/authentication/accconfirm.md
- Product: aspnet-core
- GitHub Login: @Rick-Anderson
- Microsoft Alias: riande
Hello @MSIH ... These topics build upon each other and make heavy use of underlying concepts. The Configuration and Options topics explain how the configuration/options values that are supplied by the dev make their way into code where they're used (configuration providers; loading the config values into an options class; accessing the values from the options class in code).
You really do need to access the Configuration topic to implement a configuration provider to supply the SendGrid values that best matches your needs. We're often not specific about which configuration provider to use for a few reasons:
- Although appsettings files are commonly used, devs may not want to use a file provider for config values. We don't necessarily want to suggest a specific config provider (or worse, accidentally imply a recommendation when we don't have one).
- Security: When keys like this are sensitive and the server environment isn't deemed by the organization to be safe for key storage, the dev might want to go with the Azure Key Vault config provider.
- The subject of config (and options) covers a large, flexible set of scenarios. We don't have the space in these topics to repeat a lot of content from the Configuration topic.
For local development, the secrets.json file holds the config for the Secret Manager, which loads the key-value pairs into configuration at app startup:
{
"SendGridUser": "RickAndMSFT",
"SendGridKey": "<key removed>"
}
You just need to make sure that those key-values are supplied by any implemented configuration provider ... even one that you create yourself. :point_left: That's the tiny piece of info that might have clarified things for you.
The SendGrid config will be loaded into configuration along with everything else loaded into configuration at startup. Locally, the Secret Manager will do it. In the Production environment, the Configuration API will load them from whatever config provider you use to supply them.
There's an Options class ...
public class AuthMessageSenderOptions
{
public string SendGridUser { get; set; }
public string SendGridKey { get; set; }
}
The following line in Startup.ConfigureServices will load the two key-value pairs from configuration into the Options class:
services.Configure<AuthMessageSenderOptions>(Configuration);
... and thus, they will be available to the code ...
public EmailSender(IOptions<AuthMessageSenderOptions> optionsAccessor)
{
Options = optionsAccessor.Value;
}
// Code gets the key from Options.SendGridKey
@MSIH the links send you off too soon, the following section provides complete instructions. Fixed in #7958
I’ve written a sample app following the tutorial “Account confirmation and password recovery”. After getting the SendGrid tutorial to work I’m trying to integrate it into my sample app. When I run the sample app, click on Register, an email gets sent through SendGrid, I get it in my inbox but it is not the Password Confirmation email that is referenced in Register.cshtml.cs. Is this discussion about the Options class what I need to get information from the OnPostAsync in Register.cshtml.cs to EmailSender.cs?
For deployment where should the API keys be placed then? Thank you
Hello @Kool-Koder ... This issue is closed. Please navigate to the topic at https://docs.microsoft.com/aspnet/core/security/authentication/accconfirm and use the Content feedback button at the bottom of the topic to open a new issue with your question.