AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

How to set SendGrid API Key in the code

Open MSIH opened this issue 8 years ago • 5 comments

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.

MSIH avatar Aug 05 '18 12:08 MSIH

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

guardrex avatar Aug 05 '18 15:08 guardrex

@MSIH the links send you off too soon, the following section provides complete instructions. Fixed in #7958

Rick-Anderson avatar Aug 05 '18 15:08 Rick-Anderson

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?

paul000 avatar Aug 09 '18 19:08 paul000

For deployment where should the API keys be placed then? Thank you

Kool-Koder avatar Dec 24 '18 10:12 Kool-Koder

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.

guardrex avatar Dec 24 '18 13:12 guardrex