|
| 1 | +--- |
| 2 | +ms.topic: include |
| 3 | +ms.date: 08/15/2024 |
| 4 | +--- |
| 5 | +[DefaultAzureCredential](../authentication/credential-chains.md#defaultazurecredential-overview) is an opinionated, ordered sequence of mechanisms for authenticating to Microsoft Entra ID. Each authentication mechanism is a class derived from the [TokenCredential](/dotnet/api/azure.core.tokencredential?view=azure-dotnet&preserve-view=true) class and is known as a *credential*. At runtime, `DefaultAzureCredential` attempts to authenticate using the first credential. If that credential fails to acquire an access token, the next credential in the sequence is attempted, and so on, until an access token is successfully obtained. In this way, your app can use different credentials in different environments without writing environment-specific code. |
| 6 | + |
| 7 | +To use `DefaultAzureCredential`, add the [Azure.Identity](/dotnet/api/azure.identity) and optionally the [Microsoft.Extensions.Azure](/dotnet/api/microsoft.extensions.azure) packages to your application: |
| 8 | + |
| 9 | +### [Command Line](#tab/command-line) |
| 10 | + |
| 11 | +In a terminal of your choice, navigate to the application project directory and run the following commands: |
| 12 | + |
| 13 | +```dotnetcli |
| 14 | +dotnet add package Azure.Identity |
| 15 | +dotnet add package Microsoft.Extensions.Azure |
| 16 | +``` |
| 17 | + |
| 18 | +### [NuGet Package Manager](#tab/nuget-package) |
| 19 | + |
| 20 | +Right-click your project in Visual Studio's **Solution Explorer** window and select **Manage NuGet Packages**. Search for **Azure.Identity**, and install the matching package. Repeat this process for the **Microsoft.Extensions.Azure** package. |
| 21 | + |
| 22 | +:::image type="content" source="../media/nuget-azure-identity.png" alt-text="Install a package using the package manager."::: |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +Azure services are accessed using specialized client classes from the various Azure SDK client libraries. These classes and your own custom services should be registered so they can be accessed via dependency injection throughout your app. In `Program.cs`, complete the following steps to register a client class and `DefaultAzureCredential`: |
| 27 | + |
| 28 | +1. Include the `Azure.Identity` and `Microsoft.Extensions.Azure` namespaces via `using` directives. |
| 29 | +1. Register the Azure service client using the corresponding `Add`-prefixed extension method. |
| 30 | +1. Pass an instance of `DefaultAzureCredential` to the `UseCredential` method. |
| 31 | + |
| 32 | +> [!NOTE] |
| 33 | +> For a user-assigned managed identity, make sure to assign the identity's `clientId` value to the `ManagedIdentityClientId` property on the `DefaultAzureCredentialOptions` object. This enables your code to discover the correct identity to use for authentication while running in azure. |
| 34 | +
|
| 35 | +For example: |
| 36 | + |
| 37 | +```c# |
| 38 | +using Microsoft.Extensions.Azure; |
| 39 | +using Azure.Identity; |
| 40 | + |
| 41 | +builder.Services.AddAzureClients(clientBuilder => |
| 42 | +{ |
| 43 | + clientBuilder.AddBlobServiceClient( |
| 44 | + new Uri("https://<account-name>.blob.core.windows.net")); |
| 45 | + clientBuilder.UseCredential(new DefaultAzureCredential( |
| 46 | + new DefaultAzureCredentialOptions() |
| 47 | + { |
| 48 | + ManagedIdentityClientId = "<your-client-id>" |
| 49 | + })); |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +An alternative to `UseCredential` is to instantiate `DefaultAzureCredential` directly: |
| 54 | + |
| 55 | +```c# |
| 56 | +using Azure.Identity; |
| 57 | + |
| 58 | +builder.Services.AddSingleton<BlobServiceClient>(_ => |
| 59 | + new BlobServiceClient( |
| 60 | + new Uri("https://<account-name>.blob.core.windows.net"), |
| 61 | + new DefaultAzureCredential(new DefaultAzureCredentialOptions() |
| 62 | + { |
| 63 | + ManagedIdentityClientId = "<your-client-id>" |
| 64 | + }))); |
| 65 | +``` |
| 66 | + |
| 67 | +When the preceding code runs on your local development workstation, `DefaultAzureCredential` looks in the environment variables for an application service principal or at locally installed developer tools, such as Visual Studio, for a set of developer credentials. Either approach can be used to authenticate the app to Azure resources during local development. |
| 68 | + |
| 69 | +When deployed to Azure, this same code can also authenticate your app to other Azure resources. `DefaultAzureCredential` can retrieve environment settings and managed identity configurations to authenticate to other services automatically. |
0 commit comments