Skip to content

Commit 343c867

Browse files
committed
acrolinx, linter, documentor
1 parent 945583d commit 343c867

1 file changed

Lines changed: 34 additions & 33 deletions

File tree

articles/redis/aspnet.md

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Create an ASP.NET Core web app with an Azure Redis cache
2+
title: Create an ASP.NET web app with an Azure Managed Redis cache
33
description: In this quickstart, you learn how to create an ASP.NET Core web app with an Azure Redis cache.
44
ms.date: 01/30/2026
55
ms.topic: quickstart
@@ -10,23 +10,23 @@ appliesto:
1010
# Customer intent: As an ASP.NET developer, new to Azure Redis, I want to create a new Node.js app that uses Azure Managed Redis or Azure Cache for Redis.
1111
---
1212

13-
# Azure Managed Redis Sample - ASP.NET Core Web API
13+
# Azure Managed Redis sample - ASP.NET Core Web API
1414

15-
This sample demonstrates how to connect an ASP.NET Core Web API to **Azure Managed Redis** using **Microsoft Entra ID authentication** (formerly Azure Active Directory) with the `DefaultAzureCredential` flow. The application avoids traditional connection string-based authentication in favor of token-based, identity-driven access—aligning with modern security best practices.
15+
This sample shows how to connect an ASP.NET Core Web API to Azure Managed Redis by using Microsoft Entra ID authentication with the `DefaultAzureCredential` flow. The application avoids traditional connection string-based authentication in favor of token-based, Microsoft Entra ID access, which aligns with modern security best practices.
1616

1717
## Overview
1818

1919
The application is a minimal ASP.NET Core 8.0 Web API that:
2020

21-
1. Establishes a secure, authenticated connection to Azure Managed Redis at startup
22-
2. Exposes a simple REST endpoint that reads and writes data to the cache
23-
3. Demonstrates proper Redis connection lifecycle management with dependency injection
21+
1. Establishes a secure, authenticated connection to Azure Managed Redis at startup.
22+
1. Exposes a simple REST endpoint that reads and writes data to the cache.
23+
1. Demonstrates proper Redis connection lifecycle management by using dependency injection.
2424

2525
## Prerequisites
2626

2727
- [.NET 8.0 SDK](https://dotnet.microsoft.com/download/dotnet/8.0)
2828
- An **Azure Managed Redis** instance provisioned in your Azure subscription
29-
- Your Azure user or service principal must have the appropriate **Data Access Policy** assigned on the Redis resource (e.g., `Data Owner`, `Data Contributor`, or a custom policy with read/write permissions)
29+
- Your Azure user or service principal must have the appropriate **Data Access Policy** assigned on the Redis resource, such as `Data Owner`, `Data Contributor`, or a custom policy with read and write permissions
3030
- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli) for local development authentication
3131

3232
## Required NuGet Packages
@@ -44,7 +44,7 @@ Install the primary package:
4444
dotnet add package Microsoft.Azure.StackExchangeRedis
4545
```
4646

47-
This package transitively brings in `StackExchange.Redis` and `Azure.Identity`.
47+
This package brings in `StackExchange.Redis` and `Azure.Identity` as dependencies.
4848

4949
## Configuration
5050

@@ -58,7 +58,8 @@ The application reads the Redis endpoint from configuration. Update `appsettings
5858
}
5959
```
6060

61-
> **Note:** Azure Managed Redis uses port `10000` by default. The endpoint format follows `<cache-name>.<region>.redis.azure.net:10000`.
61+
> [!NOTE]
62+
> Azure Managed Redis uses port `10000` by default. The endpoint format follows `<cache-name>.<region>.redis.azure.net:10000`.
6263
6364
## Authentication Flow
6465

@@ -70,23 +71,23 @@ Before running the application locally, authenticate with Azure:
7071
az login
7172
```
7273

73-
The `DefaultAzureCredential` will automatically pick up your Azure CLI credentials and use them to obtain an access token for the Redis resource. This eliminates the need to manage or rotate secrets locally.
74+
The `DefaultAzureCredential` automatically picks up your Azure CLI credentials and uses them to get an access token for the Redis resource. This approach eliminates the need to manage or rotate secrets locally.
7475

75-
### Production Environments
76+
### Production environments
7677

77-
In Azure-hosted environments (App Service, Container Apps, AKS, etc.), `DefaultAzureCredential` will leverage:
78+
In Azure-hosted environments such as App Service, Container Apps, and AKS, `DefaultAzureCredential` uses:
7879

7980
- **Managed Identity** (system-assigned or user-assigned)
8081
- **Workload Identity** (for Kubernetes scenarios)
8182
- **Environment variables** (for service principal authentication)
8283

83-
No code changes are required—the same `DefaultAzureCredential` seamlessly adapts to the environment.
84+
You don't need to change your code. The same `DefaultAzureCredential` seamlessly adapts to the environment.
8485

8586
## Architecture
8687

87-
### Redis Service (`Services/Redis.cs`)
88+
### Redis service (`Services/Redis.cs`)
8889

89-
The `Redis` class encapsulates the connection lifecycle:
90+
The `Redis` class manages the connection lifecycle:
9091

9192
```csharp
9293
var options = new ConfigurationOptions()
@@ -102,13 +103,13 @@ _connection = await ConnectionMultiplexer.ConnectAsync(options);
102103

103104
Key points:
104105

105-
- `ConfigureForAzureWithTokenCredentialAsync` is the extension method from `Microsoft.Azure.StackExchangeRedis` that configures token-based authentication
106-
- The `DefaultAzureCredential` handles the token acquisition and refresh automatically
107-
- The connection is established once at startup and shared across requests
106+
- `ConfigureForAzureWithTokenCredentialAsync` is an extension method from `Microsoft.Azure.StackExchangeRedis` that sets up token-based authentication
107+
- `DefaultAzureCredential` automatically handles token acquisition and refresh
108+
- The app establishes the connection once at startup and shares it across requests
108109

109-
### Dependency Injection (`Program.cs`)
110+
### Dependency injection (`Program.cs`)
110111

111-
The Redis service is registered as a singleton and initialized during application startup:
112+
The app registers the Redis service as a singleton and initializes it during startup:
112113

113114
```csharp
114115
builder.Services.AddSingleton<Redis>();
@@ -128,25 +129,25 @@ The controller injects the `Redis` service and demonstrates basic cache operatio
128129

129130
- **GET `/Sample`**: Reads the previous visit timestamp from the cache and updates it with the current time
130131

131-
## Running the Application
132+
## Running the application
132133

133134
1. Ensure you're authenticated:
134135

135136
```bash
136137
az login
137138
```
138139

139-
1. Update the Redis endpoint in `appsettings.Development.json`
140+
1. Update the Redis endpoint in `appsettings.Development.json`.
140141

141142
1. Run the application:
142143

143144
```bash
144145
dotnet run
145146
```
146147

147-
1. Navigate to `https://localhost:<port>/swagger` to access the Swagger UI
148+
1. Navigate to `https://localhost:<port>/swagger` to access the Swagger UI.
148149

149-
## Expected Output
150+
## Expected output
150151

151152
When invoking the `GET /Sample` endpoint:
152153

@@ -163,32 +164,32 @@ Previous visit was at: 2026-01-30T14:23:45
163164
(Returns the ISO 8601 formatted timestamp of the previous request)
164165
```
165166

166-
The console logs will display:
167+
The console logs display:
167168

168169
```bash
169170
info: Microsoft.Azure.StackExchangeRedis.Sample.AspNet.Controllers.SampleController
170171
Handled GET request. Previous visit time: 2026-01-30T14:23:45
171172
```
172173

173-
## Key Implementation Details
174+
## Key implementation details
174175

175-
1. **Token Refresh**: The `Microsoft.Azure.StackExchangeRedis` library automatically handles token refresh before expiration—no manual intervention required.
176+
- **Token refresh**: The `Microsoft.Azure.StackExchangeRedis` library automatically refreshes tokens before they expire, so you don't need to handle refresh manually.
176177

177-
1. **Connection Resilience**: The `ConnectionMultiplexer` from StackExchange.Redis handles reconnection logic internally.
178+
- **Connection resilience**: The `ConnectionMultiplexer` from StackExchange.Redis manages reconnection logic on its own.
178179

179-
1. **Resource Cleanup**: The `Redis` service implements `IDisposable` to properly close the connection when the application shuts down.
180+
- **Resource cleanup**: The `Redis` service implements `IDisposable` to properly close the connection when the application shuts down.
180181

181-
1. **Logging Integration**: The Redis client integrates with .NET's `ILoggerFactory` for unified logging output.
182+
- **Logging integration**: The Redis client works with .NET's `ILoggerFactory` for unified logging output.
182183

183184
## Troubleshooting
184185

185186
| Issue | Resolution |
186187
| ------- | ------------ |
187-
| `No connection is available` | Verify the endpoint format and port (`10000`). Ensure the Redis instance is provisioned and accessible. |
188+
| `No connection is available` | Verify the endpoint format and port (`10000`). Make sure the Redis instance is provisioned and accessible. |
188189
| `AuthenticationFailedException` | Run `az login` to refresh credentials. Verify your identity has the required Data Access Policy on the Redis resource. |
189-
| `Unauthorized` | Ensure your Microsoft Entra ID identity is assigned a data access role on the Azure Managed Redis instance. |
190+
| `Unauthorized` | Ensure your Microsoft Entra ID identity is assigned to a data access role on the Azure Managed Redis instance. |
190191

191-
## Additional Resources
192+
## Related content
192193

193194
- [Azure Managed Redis documentation](https://learn.microsoft.com/azure/azure-cache-for-redis/)
194195
- [Microsoft Entra ID authentication for Azure Cache for Redis](https://learn.microsoft.com/azure/azure-cache-for-redis/cache-azure-active-directory-for-authentication)

0 commit comments

Comments
 (0)