Skip to content

Commit 5b17c7f

Browse files
Apply suggestions from code review
Co-authored-by: Scott Addie <[email protected]>
1 parent 4130ee5 commit 5b17c7f

File tree

2 files changed

+14
-27
lines changed

2 files changed

+14
-27
lines changed

docs/azure/sdk/aspnetcore-guidance.md

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.custom: devx-track-dotnet
66
ms.date: 10/22/2024
77
---
88

9-
# Implement the Azure SDK for .NET in ASP.NET Core apps
9+
# Use the Azure SDK for .NET in ASP.NET Core apps
1010

1111
The Azure SDK for .NET enables ASP.NET Core apps to integrate with many different Azure services. In this article, you'll learn best practices and the steps to implement the Azure SDK for .NET in your ASP.NET Core apps. You'll learn how to:
1212

@@ -17,17 +17,17 @@ The Azure SDK for .NET enables ASP.NET Core apps to integrate with many differen
1717

1818
## Explore common Azure SDK client libraries
1919

20-
ASP.NET Core apps that connect to Azure services generally depend on the following client libraries:
20+
ASP.NET Core apps that connect to Azure services generally depend on the following Azure SDK client libraries:
2121

2222
- [Microsoft.Extensions.Azure](https://www.nuget.org/packages/Microsoft.Extensions.Azure) provides helper methods to properly register your services and handles various concerns for you, such as setting up logging, handling service lifetimes, and authentication credential management.
2323
- [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) enables Microsoft Entra ID authentication support across the Azure SDK. It provides a set of [TokenCredential](/dotnet/api/azure.core.tokencredential?view=azure-dotnet) implementations to construct Azure SDK clients that support Microsoft Entra authentication.
24-
- `Azure.<service-namespace>` libraries such as [Azure.Storage.Blob](https://www.nuget.org/packages/Azure.Storage.Blobs) and [Azure.Messaging.ServiceBus](https://www.nuget.org/packages/Azure.Messaging.ServiceBus) provide service clients and other types to help you connect to and consume specific Azure services.
24+
- `Azure.<service-namespace>` libraries, such as [Azure.Storage.Blob](https://www.nuget.org/packages/Azure.Storage.Blobs) and [Azure.Messaging.ServiceBus](https://www.nuget.org/packages/Azure.Messaging.ServiceBus), provide service clients and other types to help you connect to and consume specific Azure services. For a complete inventory of these libraries, see [Libraries using Azure.Core](/dotnet/azure/sdk/packages#libraries-using-azurecore).
2525

2626
In the sections ahead, you'll explore how to implement these libraries in an ASP.NET Core app.
2727

2828
## Register service clients
2929

30-
The Azure SDK for .NET provides service clients to connect your app to Azure services such as Azure Blob Storage and Azure Key Vault. Register these services with the dependency container in the `Program.cs` file of your app to make them available to your app using Dependency Injection.
30+
The Azure SDK for .NET client libraries provide service clients to connect your app to Azure services such as Azure Blob Storage and Azure Key Vault. Register these services with the dependency container in the `Program.cs` file of your app to make them available to your app via [dependency injection](/aspnet/core/fundamentals/dependency-injection).
3131

3232
Complete the following steps to register the services you need:
3333

@@ -45,7 +45,7 @@ Complete the following steps to register the services you need:
4545
dotnet add package Azure.Messaging.ServiceBus
4646
```
4747
48-
3. In the `Program.cs` file of your app, invoke the `AddAzureClients` extension method from the `Microsoft.Extensions.Azure` library to register a client for each service. Some services use additional subclients, which you can also register for dependency injection.
48+
3. In the `Program.cs` file of your app, invoke the `AddAzureClients` extension method from the `Microsoft.Extensions.Azure` library to register a client for each service. Some services use additional subclients, which you can also register for dependency injection via the `AddClient` extension method.
4949
5050
:::code language="csharp" source="snippets/aspnetcore-guidance/BlazorSample/Program.cs" range="11-30" highlight="4-7,11-16":::
5151
@@ -59,46 +59,32 @@ Complete the following steps to register the services you need:
5959
<!-- markdownlint-disable MD023 -->
6060
## [Blazor](#tab/blazor)
6161
62-
:::code language="cshtml" source="snippets/aspnetcore-guidance/BlazorSample/Components/Pages/Home.razor" range="1-28" highlight="5,21":::
62+
:::code language="razor" source="snippets/aspnetcore-guidance/BlazorSample/Components/Pages/Home.razor" range="1-28" highlight="5,21":::
6363
6464
---
6565
66-
Visit the [Dependency injection with the Azure SDK for .NET](https://review.learn.microsoft.com/en-us/dotnet/azure/sdk/dependency-injection) page for more detailed guidance.
66+
For more information, see [Dependency injection with the Azure SDK for .NET](/dotnet/azure/sdk/dependency-injection).
6767
6868
## Authenticate using Microsoft Entra ID
6969
7070
[Microsoft Entra ID](/entra/fundamentals/whatis) is the recommended approach to authenticate requests to Azure services. This identity service supports [role-based access control (RBAC)](/azure/role-based-access-control/overview) to manage access to Azure resources based on a user's Entra ID account and assigned roles.
7171
72-
Use the [Azure.Identity](/dotnet/api/overview/azure/identity-readme) client library to implement secretless connections to Azure services in your code with Microsoft Entra ID. The Azure Identity client library provides tools such as [`DefaultAzureCredential`](/dotnet/api/azure.identity.defaultazurecredential) to simplify configuring secure connections. `DefaultAzureCredential` supports multiple authentication methods and determines which method should be used at runtime. This approach enables your app to use different authentication methods in different environments (local vs. production) without implementing environment-specific code. Visit the [Authentication](/dotnet/azure/sdk/authentication) section of the Azure SDK for .NET docs for more details on these topics.
72+
Use the [Azure Identity](/dotnet/api/overview/azure/identity-readme) client library to implement secretless connections to Azure services in your code with Microsoft Entra ID. The Azure Identity client library provides tools such as [`DefaultAzureCredential`](/dotnet/api/azure.identity.defaultazurecredential) to simplify configuring secure connections. `DefaultAzureCredential` supports multiple authentication methods and determines which method should be used at runtime. This approach enables your app to use different authentication methods in different environments (local vs. production) without implementing environment-specific code. Visit the [Authentication](/dotnet/azure/sdk/authentication) section of the Azure SDK for .NET docs for more details on these topics.
7373
7474
> [!NOTE]
75-
> Many Azure services also allow you to authorize requests using secrets keys. However, this approach should be used with caution. Developers must be diligent to never expose the access key in an unsecure location. Anyone who has the access key is able to authorize requests against the service and data.
75+
> Many Azure services also allow you to authorize requests using keys. However, this approach should be used with caution. Developers must be diligent to never expose the access key in an unsecure location. Anyone who has the access key can authorize requests against the associated Azure resource.
7676
7777
Consider the following use of `DefaultAzureCredential`:
7878
7979
:::code language="csharp" source="snippets/aspnetcore-guidance/BlazorSample/Program.cs" range="11-30" highlight="19":::
8080
81-
In the preceding code, the `UseCredential()` method accepts an instance of `DefaultAzureCredential` to reuse across your registered services. `DefaultAzureCredential` discovers available credentials in the current environment and use them to connect to Azure services. The complete order and locations that `DefaultAzureCredential` looks for credentials lives in the [`Azure Identity library overview`](/dotnet/api/overview/azure/Identity-readme#defaultazurecredential).
82-
83-
For example, when you run the app locally, `DefaultAzureCredential` discovers and uses credentials from the following developer tools:
84-
85-
- Environment variables
86-
- Visual Studio
87-
- Azure CLI
88-
- Azure PowerShell
89-
- Azure Developer CLI
90-
91-
`DefaultAzureCredential` also discovers credentials after you deploy your app from the following:
92-
93-
- Environment variables
94-
- Workload identity
95-
- Managed identity
81+
In the preceding code, the `UseCredential` method accepts an instance of `DefaultAzureCredential` to reuse across your registered services. `DefaultAzureCredential` discovers available credentials in the current environment and use them to connect to Azure services. The order and locations in which `DefaultAzureCredential` looks for credentials lives in the [DefaultAzureCredential overview](/dotnet/azure/sdk/authentication/credential-chains?tabs=dac#defaultazurecredential-overview).
9682
9783
## Apply configurations
9884
9985
Azure SDK service clients support configurations to change their default behaviors. There are two ways to configure service clients:
10086
101-
- [Configuration files](/dotnet/core/extensions/configuration-providers#json-configuration-provider) are generally the recommended approach because they simplify app deployments between environments and reduce hard coded values.
87+
- [JSON configuration files](/dotnet/core/extensions/configuration-providers#json-configuration-provider) are generally the recommended approach because they simplify app deployments between environments and reduce hardcoded values.
10288
- Inline code configurations can be applied when you register the service client. For example, in the [Register clients and subclients](#register-service-clients) section, you explicitly passed the URI variables to the client constructors.
10389
10490
Complete the steps in the following sections to update your app to use JSON file configuration for the appropriate environments. Use the `appsettings.Development.json` file for development settings and the `appsettings.Production.json` file for production environment settings. You can add any properties from the [`ClientOptions`](/dotnet/api/azure.core.clientoptions) class to the JSON file.

docs/azure/sdk/snippets/aspnetcore-guidance/BlazorSample/Components/Pages/Home.razor

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@using Azure.Storage.Blobs
33
@using Azure.Storage.Blobs.Models
44

5-
@inject BlobServiceClient blobservice
5+
@inject BlobServiceClient blobServiceClient
66

77
<h1>Reports</h1>
88

@@ -18,7 +18,8 @@
1818

1919
protected override async Task OnInitializedAsync()
2020
{
21-
BlobContainerClient containerClient = blobservice.GetBlobContainerClient("reports");
21+
BlobContainerClient containerClient =
22+
blobServiceClient.GetBlobContainerClient("reports");
2223

2324
await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
2425
{

0 commit comments

Comments
 (0)