Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
ms.topic: include
ms.date: 04/25/2025
ms.date: 07/25/2025
---

## Authenticate to Azure services from your app
Expand Down Expand Up @@ -37,7 +37,3 @@ Azure services are accessed using specialized client classes from the various Az
1. Pass an instance of `DefaultAzureCredential` to the `UseCredential` method.
Copy link
Member

@jsquire jsquire Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This happens by default. The only time that you need to call UseCredential on the builder is when you want to override the DefaultAzureCredential that it creates on your behalf. You could also call the chained WithCredential as part of the client registration to override the default credential for just that client.

builder.Services.AddAzureClients(clientBuilder =>
{
    clientBuilder
        .AddBlobServiceClient(new Uri("https://<account-name>.blob.core.windows.net"))
        .WithCredential(new AzureCliCredential());
});


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

An alternative to the `UseCredential` method is to provide the credential to the service client directly:

:::code language="csharp" source="../snippets/authentication/local-dev-account/Program.cs" id="snippet_DefaultAzureCredential":::
6 changes: 1 addition & 5 deletions docs/azure/sdk/includes/implement-service-principal.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
ms.topic: include
ms.date: 02/12/2025
ms.date: 07/25/2025
---

[!INCLUDE [implement-service-principal-concepts](implement-service-principal-concepts.md)]
Expand Down Expand Up @@ -34,7 +34,3 @@ Azure services are accessed using specialized client classes from the various Az
1. Pass the `ClientSecretCredential` instance to the `UseCredential` method.

:::code language="csharp" source="../snippets/authentication/local-dev-service-principal/Program.cs" id="snippet_ClientSecretCredential_UseCredential":::

An alternative to the `UseCredential` method is to provide the credential to the service client directly:

:::code language="csharp" source="../snippets/authentication/local-dev-service-principal/Program.cs" id="snippet_ClientSecretCredential":::
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Azure.Identity;
using Microsoft.Extensions.Azure;
using Azure.Storage.Blobs;
using Azure.Core;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -54,13 +53,6 @@ void registerUsingServicePrincipal(WebApplicationBuilder builder)
clientBuilder.UseCredential(new DefaultAzureCredential());
});
#endregion snippet_DefaultAzureCredential_UseCredential

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

internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Azure.Identity;
using Microsoft.Extensions.Azure;
using Azure.Storage.Blobs;
using Azure.Core;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -58,17 +57,6 @@ void registerUsingServicePrincipal(WebApplicationBuilder builder)
clientBuilder.UseCredential(new ClientSecretCredential(tenantId, clientId, clientSecret));
});
#endregion snippet_ClientSecretCredential_UseCredential

#region snippet_ClientSecretCredential
var tenantId = Environment.GetEnvironmentVariable("AZURE_TENANT_ID");
var clientId = Environment.GetEnvironmentVariable("AZURE_CLIENT_ID");
var clientSecret = Environment.GetEnvironmentVariable("AZURE_CLIENT_SECRET");

builder.Services.AddSingleton<BlobServiceClient>(_ =>
new BlobServiceClient(
new Uri("https://<account-name>.blob.core.windows.net"),
new ClientSecretCredential(tenantId, clientId, clientSecret)));
#endregion snippet_ClientSecretCredential
}

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