Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions docs/azure/sdk/authentication/local-development-dev-accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Authenticate .NET apps to Azure using developer accounts
description: Learn how to authenticate your application to Azure services when using the Azure SDK for .NET during local development using developer accounts.
ms.topic: how-to
ms.date: 03/14/2025
ms.date: 11/25/2025
ms.custom:
- devx-track-dotnet
- engagement-fy23
Expand Down Expand Up @@ -122,4 +122,36 @@ Connect-AzAccount -UseDeviceAuthentication

---

[!INCLUDE [Implement DefaultAzureCredential](<../includes/implement-defaultazurecredential.md>)]
## Authenticate to Azure services from your app

The [Azure Identity library](/dotnet/api/azure.identity?view=azure-dotnet&preserve-view=true) provides *credentials*&mdash;implementations of <xref:Azure.Core.TokenCredential> that support different scenarios and Microsoft Entra authentication flows. The steps ahead demonstrate how to use <xref:Azure.Identity.DefaultAzureCredential> or a specific development tool credential when working with user accounts locally.

### Implement the code

Complete the following steps:

1. Install the [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) and the [Microsoft.Extensions.Azure](https://www.nuget.org/packages/Microsoft.Extensions.Azure) packages in your project:

```dotnetcli
dotnet add package Azure.Identity
dotnet add package Microsoft.Extensions.Azure
```

1. In `Program.cs`, add `using` directives for the `Azure.Identity` and `Microsoft.Extensions.Azure` namespaces.

1. Register the Azure service client using the corresponding `Add`-prefixed extension method.

Azure services are accessed using specialized client classes from the Azure SDK client libraries. Register these client types so you can access them through dependency injection across your app.

1. Pass a `TokenCredential` instance to the `UseCredential` method. A couple common `TokenCredential` examples include:

- A `DefaultAzureCredential` instance optimized for local development. This example sets environment variable `AZURE_TOKEN_CREDENTIALS` to `dev`. For more information, see [Exclude a credential type category](credential-chains.md#exclude-a-credential-type-category).

:::code language="csharp" source="../snippets/authentication/local-dev-account/Program.cs" id="snippet_DefaultAzureCredentialDev":::

- An instance of a credential corresponding to a specific development tool, such as `VisualStudioCredential`.

:::code language="csharp" source="../snippets/authentication/local-dev-account/Program.cs" id="snippet_VisualStudioCredential":::

> [!TIP]
> When your team uses multiple development tools to authenticate with Azure, prefer a local development-optimized instance of `DefaultAzureCredential` over tool-specific credentials.
43 changes: 0 additions & 43 deletions docs/azure/sdk/includes/implement-defaultazurecredential.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<PackageVersion Include="Azure.AI.OpenAI" Version="2.1.0" />
<PackageVersion Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25114.11" />
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="9.3.0-preview.1.25114.11" />
<PackageVersion Include="Microsoft.Extensions.Azure" Version="1.13.0" />
<PackageVersion Include="Azure.Identity" Version="1.17.0" />
<PackageVersion Include="Azure.Identity.Broker" Version="1.3.0" />
<PackageVersion Include="Microsoft.Extensions.Azure" Version="1.13.1" />
<PackageVersion Include="Azure.Identity" Version="1.17.1" />
<PackageVersion Include="Azure.Identity.Broker" Version="1.3.1" />
<PackageVersion Include="Azure.Security.KeyVault.Secrets" Version="4.8.0" />
<PackageVersion Include="Azure.Storage.Blobs" Version="12.26.0" />
<!-- ASP.NET Core packages -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Azure.Identity;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Azure;
using Azure.Storage.Blobs;
using Azure.Core;
using Azure.Identity;

var builder = WebApplication.CreateBuilder(args);

registerUsingServicePrincipal(builder);
registerUsingUserPrincipal(builder);

var app = builder.Build();

Expand Down Expand Up @@ -43,22 +42,30 @@

app.Run();

void registerUsingServicePrincipal(WebApplicationBuilder builder)
void registerUsingUserPrincipal(WebApplicationBuilder builder)
{
#region snippet_DefaultAzureCredential_UseCredential
#region snippet_VisualStudioCredential
builder.Services.AddAzureClients(clientBuilder =>
{
clientBuilder.AddBlobServiceClient(
new Uri("https://<account-name>.blob.core.windows.net"));

VisualStudioCredential credential = new();
clientBuilder.UseCredential(credential);
});
#endregion snippet_DefaultAzureCredential_UseCredential
#endregion snippet_VisualStudioCredential

#region snippet_DefaultAzureCredential
builder.Services.AddSingleton<BlobServiceClient>(_ =>
new BlobServiceClient(
new Uri("https://<account-name>.blob.core.windows.net"),
new DefaultAzureCredential()));
#endregion snippet_DefaultAzureCredential
#region snippet_DefaultAzureCredentialDev
builder.Services.AddAzureClients(clientBuilder =>
{
clientBuilder.AddBlobServiceClient(
new Uri("https://<account-name>.blob.core.windows.net"));

DefaultAzureCredential credential = new(
DefaultAzureCredential.DefaultEnvironmentVariableName);
clientBuilder.UseCredential(credential);
});
#endregion snippet_DefaultAzureCredentialDev
}

internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
Expand Down