Introduction
When a .NET application needs to access an Azure resource, it must authenticate to Azure. Whether the application is running locally, deployed to an on-premises server, or hosted in Azure, authentication is required to securely connect to Azure services. The Azure Identity library provides a seamless way to authenticate .NET applications using token-based authentication.
Table of Contents
- Recommended App Authentication Approach
- Authentication Methods for Different Environments
- Authentication in Server Environments
- Authentication During Local Development
- Using DefaultAzureCredential in an Application
- Conclusion
Prerequisites
Before implementing authentication using the Azure Identity library, ensure you have the following:
- An Azure account with an active subscription.
- .NET 8+ installed on your development machine.
- The Azure CLI (az login required for local development).
- Required Azure services set up (Azure Key Vault, Azure App Configuration, Azure Storage, etc.).
- The following NuGet packages were installed:
dotnet add package Azure.Identity dotnet add package Microsoft.Extensions.Azure dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration
Recommended App Authentication Approach
Using token-based authentication is recommended over connection strings when authenticating to Azure resources. The Azure Identity library enables seamless authentication across different environments without requiring code changes. It supports various authentication mechanisms, including:
- Managed identities (for apps hosted in Azure)
- Application service principals (for on-premises or local development scenarios)
- Developer credentials (for local development using Azure CLI, Visual Studio, or Azure PowerShell)
Why Use Token-Based Authentication?
Token-based authentication provides several benefits over connection strings:
- Principle of Least Privilege: Apps get the minimal necessary permissions.
- Enhanced Security: Avoids exposing sensitive credentials in connection strings.
- Seamless Environment Transition: No need for manual configuration changes when moving from development to production.
- Automatic Token Management: The Azure Identity library handles acquiring and refreshing Microsoft Entra ID tokens.
Authentication Methods for Different Environments
1. Authentication in Server Environments
Depending on where the app is hosted, the authentication method varies:
| Environment | Authentication Methods |
|---|---|
| Apps Hosted In Azure | Managed Identity (System-Assigned or User-Assigned) |
| Apps Hosted on-premises | Application Service Principal |
Managed Identity (Azure Hosted Apps)
A managed identity is a feature of Azure that eliminates the need to manage credentials. An Azure-hosted app can use its managed identity to authenticate to Azure services. We have two ways to enable manage identity.
System-Assigned Managed Identity
A system-assigned managed identity is created and tied directly to an Azure resource (e.g., App Service, Virtual Machine, Function App). The identity lifecycle is managed by Azure, and it is automatically deleted when the resource is deleted.
Example. Authenticating an Azure App Service using System-Assigned Managed Identity
- Enable System Assign Managed Identity on the Azure App Service. To know more, please click here.
- Assign permissions to access required Azure resources (e.g., Key Vault, Storage, etc.).
- Use DefaultAzureCredential in the .NET application to automatically use the managed identity.
public static IHostBuilder ConfigureAppConfigAndKeyVault(this IHostBuilder hostBuilder) { hostBuilder.ConfigureAppConfiguration((context, config) => { config.AddAzureAppConfiguration(options => { options.Connect(Environment.GetEnvironmentVariable("AppConfigEndpoint")) .Select(KeyFilter.Any, LabelFilter.Null) .ConfigureKeyVault(kv => { kv.SetCredential(new DefaultAzureCredential()) }); }); }); return hostBuilder; }

Join the conversation! Your thoughts help the community grow.