liveBook · Manning

API key

As explained in ASP.NET Core in Action, 3e

An API key is a secret credential used to authenticate an application or request to a remote service.

An API key is a secret credential an application supplies when calling a remote service. The service can use it to associate a request with an account. In an ASP.NET Core application, keep the key in configuration rather than hardcoding it: you can then change it at deployment without recompiling, and you reduce the risk of committing it to source control. section 10.1 section 33.4

Choose where to keep the key before writing code that uses it:

  • For local development, use User Secrets, which stores values outside the project folder. For example: `dotnet user-secrets set "MapSettings:GoogleMapsApiKey" "<your-key>"`. section 10.2.3
  • For production, use an environment variable rather than putting the key in `appsettings.json`. Neither environment variables nor User Secrets encrypts the value; their main benefit here is avoiding accidental commits. section 10.2.3
  • Do not commit keys even to a private repository. For encrypted storage, consider a service such as Azure Key Vault; its connection details still need to be stored. section 10.2.3

Attach a key to outgoing requests

An exchange-rate client can use a custom `DelegatingHandler` to read its configured API key and add an `API-KEY` header before each request is sent. Register the handler with dependency injection, then add it to the typed client using `AddHttpMessageHandler<ApiKeyMessageHandler>()`. This keeps repeated header-setting code out of individual API calls. More complex APIs may use JWTs instead of API keys. section 33.4

Figure 33.6 You can use a custom HttpMessageHandler to modify requests before they’re sent to third-party APIs. Every request passes through the custom handler before the final handler (the HttpClientHandler) sends the request to the HTTP API. After the response is received, each handler gets a chance to inspect and modify the response.
CH33_F06_Lock3

Follow the outgoing request through the custom handler: it adds the header before the final handler sends the request to the API. section 33.4

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines the key as a secret, then shows storage choices and a handler that attaches it; defers more complex token-based APIs.
Examples
Uses a mapping API key stored through User Secrets and an exchange-rate client whose handler adds an `API-KEY` header.
What it assumes
Introduces settings and secrets from their definitions; assumes familiarity with C# services for the custom-handler example.
What you can do after
Apply it via User Secrets or environment variables and an IHttpClientFactory handler that adds the key to outgoing requests.
How it connects
Connects API keys to configuration, User Secrets, environment variables, IHttpClientFactory, and JWT-based API authentication.

Also explained in

Compare how 13 books explain API key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.