Azure.Identity
1.6.0
Prefix Reserved
Advisory: https://github.com/advisories/GHSA-5mfx-4wcx-rv27 | Severity: high |
Advisory: https://github.com/advisories/GHSA-wvxc-855f-jvrv | Severity: moderate |
Advisory: https://github.com/advisories/GHSA-m5vv-6r4h-3vj9 | Severity: moderate |
See the version list below for details.
dotnet add package Azure.Identity --version 1.6.0
NuGet\Install-Package Azure.Identity -Version 1.6.0
<PackageReference Include="Azure.Identity" Version="1.6.0" />
paket add Azure.Identity --version 1.6.0
#r "nuget: Azure.Identity, 1.6.0"
// Install Azure.Identity as a Cake Addin #addin nuget:?package=Azure.Identity&version=1.6.0 // Install Azure.Identity as a Cake Tool #tool nuget:?package=Azure.Identity&version=1.6.0
Azure Identity client library for .NET
The Azure Identity library provides Azure Active Directory token authentication support across the Azure SDK. It provides a set of TokenCredential implementations which can be used to construct Azure SDK clients which support AAD token authentication.
Source code | Package (nuget) | API reference documentation | Azure Active Directory documentation
Getting started
Install the package
Install the Azure Identity client library for .NET with NuGet:
Install-Package Azure.Identity
Prerequisites
- An Azure subscription.
- The Azure CLI can also be useful for authenticating in a development environment, creating accounts, and managing account roles.
Authenticate the client
When debugging and executing code locally it is typical for a developer to use their own account for authenticating calls to Azure services. There are several developer tools which can be used to perform this authentication in your development environment.
Authenticating via Visual Studio
Developers using Visual Studio 2017 or later can authenticate an Azure Active Directory account through the IDE. Applications using the DefaultAzureCredential
or the VisualStudioCredential
can then use this account to authenticate calls in their application when running locally.
To authenticate in Visual Studio select the Tools > Options
menu to launch the Options dialog. Then navigate to the Azure Service Authentication
options to sign in with your Azure Active Directory account.
Authenticating via Visual Studio Code
Developers using Visual Studio Code can use the Azure Account extension to authenticate via the editor. Applications using the DefaultAzureCredential
or the VisualStudioCodeCredential
can then use this account to authenticate calls in their application when running locally.
To authenticate in Visual Studio Code, ensure version 0.9.11 or earlier of the Azure Account extension is installed. To track progress toward supporting newer extension versions, see this GitHub issue. Once installed, open the Command Palette and run the Azure: Sign In command.
Authenticating via the Azure CLI
Developers coding outside of an IDE can also use the Azure CLI to authenticate. Applications using the DefaultAzureCredential
or the AzureCliCredential
can then use this account to authenticate calls in their application when running locally.
To authenticate with the Azure CLI, users can run the command az login
. For users running on a system with a default web browser the azure cli will launch the browser to authenticate the user.
For systems without a default web browser, the az login
command will use the device code authentication flow. The user can also force the Azure CLI to use the device code flow rather than launching a browser by specifying the --use-device-code
argument.
Authenticating via Azure PowerShell
Developers coding outside of an IDE can also use Azure PowerShell to authenticate. Applications using the DefaultAzureCredential
or the AzurePowerShellCredential
can then use this account to authenticate calls in their application when running locally.
To authenticate with Azure PowerShell, users can run the command Connect-AzAccount
. For users running on a system with a default web browser and version 5.0.0 or later of azure PowerShell, it will launch the browser to authenticate the user.
For systems without a default web browser, the Connect-AzAccount
command will use the device code authentication flow. The user can also force Azure PowerShell to use the device code flow rather than launching a browser by specifying the UseDeviceAuthentication
argument.
Key concepts
Credentials
A credential is a class which contains or can obtain the data needed for a service client to authenticate requests. Service clients across Azure SDK accept credentials when they are constructed, and service clients use those credentials to authenticate requests to the service.
The Azure Identity library focuses on OAuth authentication with Azure Active directory, and it offers a variety of credential classes capable of acquiring an AAD token to authenticate service requests. All of the credential classes in this library are implementations of the TokenCredential
abstract class in Azure.Core, and any of them can be used to construct service clients capable of authenticating with a TokenCredential
.
See Credential Classes for a complete listing of available credential types.
DefaultAzureCredential
The DefaultAzureCredential
is appropriate for most scenarios where the application is intended to ultimately be run in the Azure Cloud. This is because the DefaultAzureCredential
combines credentials commonly used to authenticate when deployed, with credentials used to authenticate in a development environment.
Note:
DefaultAzureCredential
is intended to simplify getting started with the SDK by handling common scenarios with reasonable default behaviors. Developers who want more control or whose scenario isn't served by the default settings should use other credential types.
The DefaultAzureCredential
will attempt to authenticate via the following mechanisms in order.
- Environment - The
DefaultAzureCredential
will read account information specified via environment variables and use it to authenticate. - Managed Identity - If the application is deployed to an Azure host with Managed Identity enabled, the
DefaultAzureCredential
will authenticate with that account. - Visual Studio - If the developer has authenticated via Visual Studio, the
DefaultAzureCredential
will authenticate with that account. - Visual Studio Code - If the developer has authenticated via the Visual Studio Code Azure Account plugin, the
DefaultAzureCredential
will authenticate with that account. - Azure CLI - If the developer has authenticated an account via the Azure CLI
az login
command, theDefaultAzureCredential
will authenticate with that account. - Azure PowerShell - If the developer has authenticated an account via the Azure PowerShell
Connect-AzAccount
command, theDefaultAzureCredential
will authenticate with that account. - Interactive - If enabled the
DefaultAzureCredential
will interactively authenticate the developer via the current system's default browser.
Examples
Authenticating with the DefaultAzureCredential
This example demonstrates authenticating the SecretClient
from the Azure.Security.KeyVault.Secrets client library using the DefaultAzureCredential
.
// Create a secret client using the DefaultAzureCredential
var client = new SecretClient(new Uri("https://myvault.vault.azure.net/"), new DefaultAzureCredential());
Enabling the interactive authentication with the DefaultAzureCredential
Interactive authentication is disabled in the DefaultAzureCredential
by default. This example demonstrates two ways of enabling the interactive authentication portion of the DefaultAzureCredential
. When enabled the DefaultAzureCredential
will fall back to interactively authenticating the developer via the system's default browser if when no other credentials are available. This example then authenticates an EventHubProducerClient
from the Azure.Messaging.EventHubs client library using the DefaultAzureCredential
with interactive authentication enabled.
// the includeInteractiveCredentials constructor parameter can be used to enable interactive authentication
var credential = new DefaultAzureCredential(includeInteractiveCredentials: true);
var eventHubClient = new EventHubProducerClient("myeventhub.eventhubs.windows.net", "myhubpath", credential);
Specifying a user assigned managed identity with the DefaultAzureCredential
Many Azure hosts allow the assignment of a user assigned managed identity. This example demonstrates configuring the DefaultAzureCredential
to authenticate a user assigned identity when deployed to an azure host. It then authenticates a BlobClient
from the Azure.Storage.Blobs client library with credential.
// When deployed to an azure host, the default azure credential will authenticate the specified user assigned managed identity.
string userAssignedClientId = "<your managed identity client Id>";
var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = userAssignedClientId });
var blobClient = new BlobClient(new Uri("https://myaccount.blob.core.windows.net/mycontainer/myblob"), credential);
In addition to configuring the ManagedIdentityClientId
via code, it can also be set using the AZURE_CLIENT_ID
environment variable. These two approaches are equivalent when using the DefaultAzureCredential
.
Define a custom authentication flow with the ChainedTokenCredential
While the DefaultAzureCredential
is generally the quickest way to get started developing applications for Azure, more advanced users may want to customize the credentials considered when authenticating. The ChainedTokenCredential
enables users to combine multiple credential instances to define a customized chain of credentials. This example demonstrates creating a ChainedTokenCredential
which will attempt to authenticate using managed identity, and fall back to authenticating via the Azure CLI if managed identity is unavailable in the current environment. The credential is then used to authenticate an EventHubProducerClient
from the Azure.Messaging.EventHubs client library.
// Authenticate using managed identity if it is available; otherwise use the Azure CLI to authenticate.
var credential = new ChainedTokenCredential(new ManagedIdentityCredential(), new AzureCliCredential());
var eventHubProducerClient = new EventHubProducerClient("myeventhub.eventhubs.windows.net", "myhubpath", credential);
Managed Identity Support
The Managed identity authentication is supported via either the DefaultAzureCredential
or the ManagedIdentityCredential
directly for the following Azure Services:
- Azure Virtual Machines
- Azure App Service and Azure Functions
- Azure Kubernetes Service
- Azure Cloud Shell
- Azure Arc
- Azure Service Fabric
Credential Classes
Authenticating Azure Hosted Applications
credential | usage |
---|---|
DefaultAzureCredential |
provides a simplified authentication experience to quickly start developing applications run in the Azure cloud |
ChainedTokenCredential |
allows users to define custom authentication flows composing multiple credentials |
ManagedIdentityCredential |
authenticates the managed identity of an azure resource |
EnvironmentCredential |
authenticates a service principal or user via credential information specified in environment variables |
Authenticating Service Principals
credential | usage |
---|---|
ClientSecretCredential |
authenticates a service principal using a secret |
ClientCertificateCredential |
authenticates a service principal using a certificate |
Authenticating Users
credential | usage |
---|---|
InteractiveBrowserCredential |
interactively authenticates a user with the default system browser |
DeviceCodeCredential |
interactively authenticates a user on devices with limited UI |
UsernamePasswordCredential |
authenticates a user with a username and password |
AuthorizationCodeCredential |
authenticate a user with a previously obtained authorization code |
Authenticating via Development Tools
credential | usage |
---|---|
AzureCliCredential |
authenticate in a development environment with the Azure CLI |
AzurePowerShellCredential |
authenticate in a development environment with the Azure PowerShell |
VisualStudioCredential |
authenticate in a development environment with Visual Studio |
VisualStudioCodeCredential |
authenticate in a development environment with Visual Studio Code |
Note: All credential implementations in the Azure Identity library are threadsafe, and a single credential instance can be used by multiple service clients.
Environment Variables
DefaultAzureCredential
and EnvironmentCredential
can be configured with environment variables. Each type of authentication requires values for specific variables:
Service principal with secret
variable name | value |
---|---|
AZURE_CLIENT_ID |
id of an Azure Active Directory application |
AZURE_TENANT_ID |
id of the application's Azure Active Directory tenant |
AZURE_CLIENT_SECRET |
one of the application's client secrets |
Service principal with certificate
variable name | value |
---|---|
AZURE_CLIENT_ID |
id of an Azure Active Directory application |
AZURE_TENANT_ID |
id of the application's Azure Active Directory tenant |
AZURE_CLIENT_CERTIFICATE_PATH |
path to a PEM-encoded certificate file including private key (without password protection) |
AZURE_CLIENT_SEND_CERTIFICATE_CHAIN |
(optional) send certificate chain in x5c header to support subject name / issuer based authentication |
Username and password
variable name | value |
---|---|
AZURE_CLIENT_ID |
id of an Azure Active Directory application |
AZURE_USERNAME |
a username (usually an email address) |
AZURE_PASSWORD |
that user's password |
Configuration is attempted in the above order. For example, if values for a client secret and certificate are both present, the client secret will be used.
Troubleshooting
See the troubleshooting guide for details on how to diagnose various failure scenarios.
Error Handling
Errors arising from authentication can be raised on any service client method which makes a request to the service. This is because the first time the token is requested from the credential is on the first call to the service, and any subsequent calls might need to refresh the token. In order to distinguish these failures from failures in the service client Azure Identity classes raise the AuthenticationFailedException
with details to the source of the error in the exception message as well as possibly the error message. Depending on the application these errors may or may not be recoverable.
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
// Create a secret client using the DefaultAzureCredential
var client = new SecretClient(new Uri("https://myvault.vault.azure.net/"), new DefaultAzureCredential());
try
{
KeyVaultSecret secret = await client.GetSecretAsync("secret1");
}
catch (AuthenticationFailedException e)
{
Console.WriteLine($"Authentication Failed. {e.Message}");
}
For more details on dealing with errors arising from failed requests to Azure Active Directory, or managed identity endpoints please refer to the Azure Active Directory documentation on authorization error codes.
Logging
The Azure Identity library provides the same logging capabilities as the rest of the Azure SDK.
The simplest way to see the logs to help debug authentication issues is to enable the console logging.
// Setup a listener to monitor logged events.
using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();
All credentials can be configured with diagnostic options, in the same way as other clients in the SDK.
CAUTION: Requests and responses in the Azure Identity library contain sensitive information. Precaution must be taken to protect logs, when customizing the output, to avoid compromising account security.
DefaultAzureCredentialOptions options = new DefaultAzureCredentialOptions
{
Diagnostics =
{
LoggedHeaderNames = { "x-ms-request-id" },
LoggedQueryParameters = { "api-version" },
IsLoggingContentEnabled = true
}
};
When troubleshooting authentication issues, you may also want to enable logging of sensitive information. To enable this type of logging, set the IsLoggingContentEnabled
property to true
. To only log details about the account that was used to attempt authentication and authorization, set IsAccountIdentifierLoggingEnabled
to true
.
DefaultAzureCredentialOptions options = new DefaultAzureCredentialOptions
{
Diagnostics =
{
LoggedHeaderNames = { "x-ms-request-id" },
LoggedQueryParameters = { "api-version" },
IsAccountIdentifierLoggingEnabled = true
}
};
Thread safety
We guarantee that all credential instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing credential instances is always safe, even across threads.
Additional concepts
Client options | Accessing the response | Diagnostics | Mocking | Client lifetime
Next steps
Client libraries supporting authentication with Azure Identity
Many of the client libraries listed here support authenticating with TokenCredential
and the Azure Identity library.
There you will also find links where you can learn more about their use, including additional documentation and samples.
Known Issues
This library does not currently support scenarios relating to the AAD B2C service.
Currently open issues for the Azure.Identity library can be found here.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Azure.Core (>= 1.24.0)
- Microsoft.Identity.Client (>= 4.39.0)
- Microsoft.Identity.Client.Extensions.Msal (>= 2.19.3)
- System.Memory (>= 4.5.4)
- System.Security.Cryptography.ProtectedData (>= 4.7.0)
- System.Text.Json (>= 4.7.2)
- System.Threading.Tasks.Extensions (>= 4.5.4)
NuGet packages (962)
Showing the top 5 NuGet packages that depend on Azure.Identity:
Package | Downloads |
---|---|
Microsoft.Data.SqlClient
The current data provider for SQL Server and Azure SQL databases. This has replaced System.Data.SqlClient. These classes provide access to SQL and encapsulate database-specific protocols, including tabular data stream (TDS). Commonly Used Types: Microsoft.Data.SqlClient.SqlConnection Microsoft.Data.SqlClient.SqlException Microsoft.Data.SqlClient.SqlParameter Microsoft.Data.SqlClient.SqlDataReader Microsoft.Data.SqlClient.SqlCommand Microsoft.Data.SqlClient.SqlTransaction Microsoft.Data.SqlClient.SqlParameterCollection Microsoft.Data.SqlClient.SqlClientFactory When using NuGet 3.x this package requires at least version 3.4. |
|
Microsoft.Extensions.Azure
Azure Client SDK integration with Microsoft.Extensions libraries |
|
Microsoft.Extensions.Caching.SqlServer
Distributed cache implementation of Microsoft.Extensions.Caching.Distributed.IDistributedCache using Microsoft SQL Server. This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/af22effae4069a5dfb9b0735859de48820104f5b |
|
Microsoft.Identity.Web.Certificate
This package brings certificate management for MSAL.NET. |
|
WindowsAzure.ServiceBus
Please note, for Azure Service Bus, Azure Event Hubs and Azure Relay, newer packages Azure.Messaging.ServiceBus, Azure.Messaging.EventHubs and Microsoft.Azure.Relay are available as of November 2020, February 2020 and March 2017 respectively. While WindowsAzure.ServiceBus will continue to receive critical bug fixes, we strongly encourage you to upgrade. Read https://aka.ms/azsdk/blog/msging/intro for more details. It adds Microsoft.ServiceBus.dll along with related configuration files to your project. This library allows AMQP 1.0 to be used as one of the protocols for communication with Microsoft Azure Service Bus. For more information on Messaging features, please visit: http://azure.microsoft.com/en-us/documentation/services/service-bus/ For more information on Event Hub features, please visit: http://go.microsoft.com/fwlink/?LinkID=403957 Please note that this package requires at least .Net Framework 4.6.2. |
GitHub repositories (172)
Showing the top 5 popular GitHub repositories that depend on Azure.Identity:
Repository | Stars |
---|---|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
jasontaylordev/CleanArchitecture
Clean Architecture Solution Template for ASP.NET Core
|
|
dotnet/efcore
EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
|
|
dotnet/AspNetCore.Docs
Documentation for ASP.NET Core
|
|
dotnet-architecture/eShopOnWeb
Sample ASP.NET Core 8.0 reference application, powered by Microsoft, demonstrating a layered application architecture with monolithic deployment model. Download the eBook PDF from docs folder.
|
Version | Downloads | Last updated | |
---|---|---|---|
1.13.1 | 1,400,402 | 10/24/2024 | |
1.13.0 | 1,160,723 | 10/15/2024 | |
1.13.0-beta.2 | 30,859 | 9/17/2024 | |
1.13.0-beta.1 | 69,751 | 7/24/2024 | |
1.12.1 | 2,548,521 | 9/26/2024 | |
1.12.0 | 22,821,492 | 6/17/2024 | |
1.12.0-beta.3 | 14,295 | 6/11/2024 | |
1.12.0-beta.2 | 52,917 | 5/7/2024 | |
1.12.0-beta.1 | 18,086 | 4/23/2024 | |
1.11.4 | 23,780,539 | 6/10/2024 | |
1.11.3 | 12,384,434 | 5/7/2024 | |
1.11.2 | 5,266,882 | 4/19/2024 | |
1.11.1 | 4,239,148 | 4/16/2024 | |
1.11.0 | 9,043,949 | 4/9/2024 | |
1.11.0-beta.1 | 62,943 | 2/6/2024 | |
1.10.4 | 40,454,827 | 11/13/2023 | |
1.10.3 | 69,885,599 | 10/18/2023 | |
1.10.2 | 13,362,804 | 10/9/2023 | |
1.10.1 | 2,271,915 | 9/13/2023 | |
1.10.0 | 3,390,757 | 8/15/2023 | |
1.10.0-beta.1 | 16,915 | 7/17/2023 | |
1.9.0 | 16,399,131 | 5/11/2023 | |
1.9.0-beta.3 | 45,842 | 4/12/2023 | |
1.9.0-beta.2 | 121,014 | 2/22/2023 | |
1.9.0-beta.1 | 310,616 | 10/13/2022 | |
1.8.2 | 18,130,103 | 2/8/2023 | |
1.8.1 | 4,196,633 | 1/13/2023 | |
1.8.0 | 12,504,104 | 11/9/2022 | |
1.8.0-beta.1 | 5,582 | 10/13/2022 | |
1.7.0 | 80,550,911 | 9/19/2022 | |
1.7.0-beta.1 | 71,983 | 8/10/2022 | |
1.6.1 | 7,141,454 | 8/8/2022 | |
1.6.0 | 85,342,142 | 4/5/2022 | |
1.6.0-beta.1 | 55,664 | 2/11/2022 | |
1.5.0 | 36,888,444 | 10/14/2021 | |
1.5.0-beta.4 | 57,058 | 9/8/2021 | |
1.5.0-beta.3 | 156,418 | 8/11/2021 | |
1.5.0-beta.2 | 37,508 | 7/12/2021 | |
1.5.0-beta.1 | 27,327 | 6/8/2021 | |
1.4.1 | 9,237,428 | 8/5/2021 | |
1.4.0 | 44,419,098 | 5/12/2021 | |
1.4.0-beta.5 | 159,904 | 4/6/2021 | |
1.4.0-beta.4 | 36,090 | 3/9/2021 | |
1.4.0-beta.3 | 137,518 | 2/10/2021 | |
1.4.0-beta.2 | 22,761 | 1/30/2021 | |
1.4.0-beta.1 | 126,388 | 10/16/2020 | |
1.3.0 | 133,818,914 | 11/13/2020 | |
1.3.0-beta.2 | 3,534 | 10/8/2020 | |
1.3.0-beta.1 | 41,129 | 9/11/2020 | |
1.2.3 | 11,076,277 | 9/11/2020 | |
1.2.2 | 1,306,912 | 8/20/2020 | |
1.2.1 | 877,267 | 8/18/2020 | |
1.2.0-preview.6 | 19,603 | 7/23/2020 | |
1.2.0-preview.5 | 16,745 | 7/8/2020 | |
1.2.0-preview.4 | 70,983 | 6/10/2020 | |
1.2.0-preview.3 | 126,428 | 5/5/2020 | |
1.2.0-preview.2 | 47,261 | 4/6/2020 | |
1.2.0-preview.1 | 72,295 | 3/10/2020 | |
1.1.1 | 13,755,230 | 2/11/2020 | |
1.1.0 | 914,132 | 11/25/2019 | |
1.0.0 | 3,078,856 | 10/30/2019 | |
1.0.0-preview.5 | 10,838 | 10/7/2019 | |
1.0.0-preview.4 | 3,618 | 9/10/2019 | |
1.0.0-preview.3 | 5,514 | 8/6/2019 | |
1.0.0-preview.2 | 3,681 | 7/2/2019 | |
1.0.0-preview.1 | 1,033 | 6/28/2019 |