|
| 1 | +--- |
| 2 | +title: Best practices for using the Azure SDK with ASP.NET Core |
| 3 | +description: Learn best practices and the steps to properly implement the Azure SDK for .NET in your ASP.NET Core apps. |
| 4 | +ms.topic: conceptual |
| 5 | +ms.custom: devx-track-dotnet |
| 6 | +ms.date: 10/22/2024 |
| 7 | +--- |
| 8 | + |
| 9 | +# Use the Azure SDK for .NET in ASP.NET Core apps |
| 10 | + |
| 11 | +The Azure SDK for .NET enables ASP.NET Core apps to integrate with many different Azure services. In this article, you'll learn best practices and the steps to adopt the Azure SDK for .NET in your ASP.NET Core apps. You'll learn how to: |
| 12 | + |
| 13 | +- Register services for dependency injection. |
| 14 | +- Authenticate to Azure without using passwords or secrets. |
| 15 | +- Implement centralized, standardized configuration. |
| 16 | +- Configure common web app concerns such as logging and retries. |
| 17 | + |
| 18 | +## Explore common Azure SDK client libraries |
| 19 | + |
| 20 | +ASP.NET Core apps that connect to Azure services generally depend on the following Azure SDK client libraries: |
| 21 | + |
| 22 | +- [Microsoft.Extensions.Azure](https://www.nuget.org/packages/Microsoft.Extensions.Azure) provides helper methods to register clients with the dependency injection service collection and handles various concerns for you, such as setting up logging, handling DI service lifetimes, and authentication credential management. |
| 23 | +- [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) enables Microsoft Entra ID authentication support across the Azure SDK. It provides a set of [TokenCredential](/dotnet/api/azure.core.tokencredential?view=azure-dotnet) implementations to construct Azure SDK clients that support Microsoft Entra authentication. |
| 24 | +- `Azure.<service-namespace>` libraries, such as [Azure.Storage.Blobs](https://www.nuget.org/packages/Azure.Storage.Blobs) and [Azure.Messaging.ServiceBus](https://www.nuget.org/packages/Azure.Messaging.ServiceBus), provide service clients and other types to help you connect to and consume specific Azure services. For a complete inventory of these libraries, see [Libraries using Azure.Core](/dotnet/azure/sdk/packages#libraries-using-azurecore). |
| 25 | + |
| 26 | +In the sections ahead, you'll explore how to implement an ASP.NET Core application that uses these libraries. |
| 27 | + |
| 28 | +## Register Azure SDK clients with the DI service collection |
| 29 | + |
| 30 | +The Azure SDK for .NET client libraries provide service clients to connect your app to Azure services such as Azure Blob Storage and Azure Key Vault. Register these services with the dependency container in the `Program.cs` file of your app to make them available via [dependency injection](/aspnet/core/fundamentals/dependency-injection). |
| 31 | + |
| 32 | +Complete the following steps to register the services you need: |
| 33 | + |
| 34 | +1. Add the [Microsoft.Extensions.Azure](https://www.nuget.org/packages/Microsoft.Extensions.Azure) package: |
| 35 | + |
| 36 | + ```dotnetcli |
| 37 | + dotnet add package Microsoft.Extensions.Azure |
| 38 | + ``` |
| 39 | +
|
| 40 | +2. Add the relevant `Azure.*` service client packages: |
| 41 | +
|
| 42 | + ```dotnetcli |
| 43 | + dotnet add package Azure.Security.KeyVault.Secrets |
| 44 | + dotnet add package Azure.Storage.Blobs |
| 45 | + dotnet add package Azure.Messaging.ServiceBus |
| 46 | + ``` |
| 47 | +
|
| 48 | +3. In the `Program.cs` file of your app, invoke the <xref:Microsoft.Extensions.Azure.AzureClientServiceCollectionExtensions.AddAzureClients%2A> extension method from the `Microsoft.Extensions.Azure` library to register a client to communicate with each Azure service. Some client libraries provide additional [subclients](https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-subclients) for specific subgroups of Azure service functionality. You can register such subclients for dependency injection via the <xref:Microsoft.Extensions.Azure.AzureClientFactoryBuilder.AddClient%2A> extension method. |
| 49 | +
|
| 50 | + :::code language="csharp" source="snippets/aspnetcore-guidance/BlazorSample/Program.cs" range="11-30" highlight="4-7,13-15"::: |
| 51 | +
|
| 52 | +4. Inject the registered clients into your ASP.NET Core app components, services, or API endpoint: |
| 53 | +
|
| 54 | + <!-- markdownlint-disable MD023 --> |
| 55 | + ## [Minimal API](#tab/api) |
| 56 | +
|
| 57 | + :::code language="csharp" source="snippets/aspnetcore-guidance/MinApiSample/Program.cs" range="52-73" highlight="1-3,6,8,11-12"::: |
| 58 | +
|
| 59 | + <!-- markdownlint-disable MD023 --> |
| 60 | + ## [Blazor](#tab/blazor) |
| 61 | +
|
| 62 | + :::code language="razor" source="snippets/aspnetcore-guidance/BlazorSample/Components/Pages/Home.razor" range="1-28" highlight="19-28"::: |
| 63 | +
|
| 64 | + --- |
| 65 | +
|
| 66 | +For more information, see [Dependency injection with the Azure SDK for .NET](/dotnet/azure/sdk/dependency-injection). |
| 67 | +
|
| 68 | +## Authenticate using Microsoft Entra ID |
| 69 | +
|
| 70 | +Token-based authentication with [Microsoft Entra ID](/entra/fundamentals/whatis) is the recommended approach to authenticate requests to Azure services. To authorize those requests, [Azure role-based access control (RBAC)](/azure/role-based-access-control/overview) manages access to Azure resources based on a user's Microsoft Entra identity and assigned roles. |
| 71 | +
|
| 72 | +Use the [Azure Identity](/dotnet/api/overview/azure/identity-readme) library for the aforementioned token-based authentication support. The library provides classes such as [`DefaultAzureCredential`](/dotnet/api/azure.identity.defaultazurecredential) to simplify configuring secure connections. `DefaultAzureCredential` supports multiple authentication methods and determines which method should be used at runtime. This approach enables your app to use different authentication methods in different environments (local vs. production) without implementing environment-specific code. Visit the [Authentication](/dotnet/azure/sdk/authentication) section of the Azure SDK for .NET docs for more details on these topics. |
| 73 | +
|
| 74 | +> [!NOTE] |
| 75 | +> Many Azure services also allow you to authorize requests using keys. However, this approach should be used with caution. Developers must be diligent to never expose the access key in an unsecure location. Anyone who has the access key can authorize requests against the associated Azure resource. |
| 76 | +
|
| 77 | +1. Add the [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) package: |
| 78 | +
|
| 79 | + ```dotnetcli |
| 80 | + dotnet add package Azure.Identity |
| 81 | + ``` |
| 82 | +
|
| 83 | +1. In the `Program.cs` file of your app, invoke the <xref:Microsoft.Extensions.Azure.AzureClientFactoryBuilder.UseCredential%2A> extension method from the `Microsoft.Extensions.Azure` library to set a shared `DefaultAzureCredential` instance for all registered Azure service clients: |
| 84 | +
|
| 85 | + :::code language="csharp" source="snippets/aspnetcore-guidance/BlazorSample/Program.cs" range="11-30" highlight="19"::: |
| 86 | +
|
| 87 | + `DefaultAzureCredential` discovers available credentials in the current environment and uses them to authenticate to Azure services. For the order and locations in which `DefaultAzureCredential` scans for credentials, see [DefaultAzureCredential overview](/dotnet/azure/sdk/authentication/credential-chains?tabs=dac#defaultazurecredential-overview). Using a shared `DefaultAzureCredential` instance ensures the underlying token cache is used, which improves application resilience and performance due to fewer requests for a new token. |
| 88 | +
|
| 89 | +## Apply configurations |
| 90 | +
|
| 91 | +Azure SDK service clients support configurations to change their default behaviors. There are two ways to configure service clients: |
| 92 | +
|
| 93 | +- [JSON configuration files](/dotnet/core/extensions/configuration-providers#json-configuration-provider) are generally the recommended approach because they simplify managing differences in app deployments between environments. |
| 94 | +- Inline code configurations can be applied when you register the service client. For example, in the [Register clients and subclients](#register-azure-sdk-clients-with-the-di-service-collection) section, you explicitly passed the URI variables to the client constructors. |
| 95 | +
|
| 96 | +`IConfiguration` precedence rules are respected by the `Microsoft.Extensions.Azure` extension methods, which are detailed in the [Configuration Providers](/dotnet/core/extensions/configuration#configuration-providers) documentation. |
| 97 | +
|
| 98 | +Complete the steps in the following sections to update your app to use JSON file configuration for the appropriate environments. Use the `appsettings.Development.json` file for development settings and the `appsettings.Production.json` file for production environment settings. You can add configuration settings whose names are public properties on the <xref:Azure.Core.ClientOptions> class to the JSON file. |
| 99 | +
|
| 100 | +### Configure registered services |
| 101 | +
|
| 102 | +1. Update the `appsettings.<environment>.json` file in your app with the highlighted service configurations: |
| 103 | +
|
| 104 | + :::code language="json" source="snippets/aspnetcore-guidance/MinApiSample/appsettings.Development.json" highlight="19-27"::: |
| 105 | +
|
| 106 | + In the preceding JSON sample: |
| 107 | +
|
| 108 | + - The top-level key names, `KeyVault`, `ServiceBus`, and `Storage`, are arbitrary names used to reference the config sections from your code. You will pass these names to `AddClient` extension methods to configure a given client. All other key names map to specific client options, and JSON serialization is performed in a case-insensitive manner. |
| 109 | + - The `KeyVault:VaultUri`, `ServiceBus:Namespace`, and `Storage:ServiceUri` key values map to the arguments of the <xref:Azure.Security.KeyVault.Secrets.SecretClient.%23ctor(System.Uri,Azure.Core.TokenCredential,Azure.Security.KeyVault.Secrets.SecretClientOptions)?displayProperty=name>, <xref:Azure.Messaging.ServiceBus.ServiceBusClient.%23ctor(System.String)?displayProperty=name>, and <xref:Azure.Storage.Blobs.BlobServiceClient.%23ctor(System.Uri,Azure.Core.TokenCredential,Azure.Storage.Blobs.BlobClientOptions)?displayProperty=name> constructor overloads, respectively. The `TokenCredential` variants of the constructors are used because a default `TokenCredential` is set via the <xref:Microsoft.Extensions.Azure.AzureClientFactoryBuilder.UseCredential(Azure.Core.TokenCredential)?displayProperty=name> method call. |
| 110 | +
|
| 111 | +1. Update the the `Program.cs` file to retrieve the JSON file configurations using `IConfiguration` and pass them into your service registrations: |
| 112 | +
|
| 113 | + :::code language="csharp" source="snippets/aspnetcore-guidance/MinApiSample/Program.cs" range="13-31" highlight="5-6,8-9,12-13"::: |
| 114 | +
|
| 115 | +### Configure Azure defaults and retries |
| 116 | +
|
| 117 | +You may want to change default Azure client configurations globally or for a specific service client. For example, you may want different retry settings or to use a different service API version. You can set the retry settings globally or on a per-service basis. |
| 118 | +
|
| 119 | +1. Update your configuration file to set default Azure settings, such as a new default retry policy that all registered Azure clients will use: |
| 120 | +
|
| 121 | + :::code language="json" source="snippets/aspnetcore-guidance/MinApiSample/appsettings.Development.json" highlight="9-18"::: |
| 122 | +
|
| 123 | +2. In the `Program.cs` file, call the `ConfigureDefaults` extension method to retrieve the default settings and apply them to your service clients: |
| 124 | +
|
| 125 | + :::code language="csharp" source="snippets/aspnetcore-guidance/MinApiSample/Program.cs" range="14-41" highlight="26-27"::: |
| 126 | +
|
| 127 | +## Configure logging |
| 128 | +
|
| 129 | +The Azure SDK for .NET client libraries can log client library operations to monitor requests and responses to Azure services. Client libraries can also log a variety of other events, including retries, token retrieval, and service-specific events from various clients. When you register an Azure SDK client using the <xref:Microsoft.Extensions.Azure.AzureClientServiceCollectionExtensions.AddAzureClients%2A> extension method, the <xref:Microsoft.Extensions.Azure.AzureEventSourceLogForwarder> is registered with the dependency injection container. The `AzureEventSourceLogForwarder` forwards log messages from Azure SDK event sources to <xref:Microsoft.Extensions.Logging.ILoggerFactory> to enables you to use the standard ASP.NET Core logging configuration for logging. |
| 130 | +
|
| 131 | +The following table depicts how the Azure SDK for .NET `EventLevel` maps to the ASP.NET Core `LogLevel`. For more information on these topics and other scenarios, see [Logging with the Azure SDK for .NET](/dotnet/azure/sdk/logging) and [Dependency injection with the Azure SDK for .NET](/dotnet/azure/sdk/dependency-injection). |
| 132 | +
|
| 133 | +| Azure SDK `EventLevel` | ASP.NET Core `LogLevel` | |
| 134 | +|------------------------|-------------------------| |
| 135 | +| `Critical` | `Critical` | |
| 136 | +| `Error` | `Error` | |
| 137 | +| `Informational` | `Information` | |
| 138 | +| `Warning` | `Warning` | |
| 139 | +| `Verbose` | `Debug` | |
| 140 | +| `LogAlways` | `Information` | |
| 141 | +
|
| 142 | +You can change default log levels and other settings using the same JSON configurations outlined in the [configure authentication](#authenticate-using-microsoft-entra-id) section. For example, toggle the `ServiceBusClient` log level to `Debug` by setting the `Logging:LogLevel:Azure.Messaging.ServiceBus` key as follows: |
| 143 | +
|
| 144 | +:::code language="json" source="snippets/aspnetcore-guidance/MinApiSample/appsettings.Development.json" highlight="6"::: |
0 commit comments