You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/redis/aspnet.md
+34-33Lines changed: 34 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
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
3
3
description: In this quickstart, you learn how to create an ASP.NET Core web app with an Azure Redis cache.
4
4
ms.date: 01/30/2026
5
5
ms.topic: quickstart
@@ -10,23 +10,23 @@ appliesto:
10
10
# 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.
11
11
---
12
12
13
-
# Azure Managed Redis Sample - ASP.NET Core Web API
13
+
# Azure Managed Redis sample - ASP.NET Core Web API
14
14
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 authenticationwith 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.
16
16
17
17
## Overview
18
18
19
19
The application is a minimal ASP.NET Core 8.0 Web API that:
20
20
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.
- 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
30
30
-[Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli) for local development authentication
This package transitively brings in `StackExchange.Redis` and `Azure.Identity`.
47
+
This package brings in `StackExchange.Redis` and `Azure.Identity` as dependencies.
48
48
49
49
## Configuration
50
50
@@ -58,7 +58,8 @@ The application reads the Redis endpoint from configuration. Update `appsettings
58
58
}
59
59
```
60
60
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`.
62
63
63
64
## Authentication Flow
64
65
@@ -70,23 +71,23 @@ Before running the application locally, authenticate with Azure:
70
71
az login
71
72
```
72
73
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.
-`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
108
109
109
-
### Dependency Injection (`Program.cs`)
110
+
### Dependency injection (`Program.cs`)
110
111
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:
112
113
113
114
```csharp
114
115
builder.Services.AddSingleton<Redis>();
@@ -128,25 +129,25 @@ The controller injects the `Redis` service and demonstrates basic cache operatio
128
129
129
130
-**GET `/Sample`**: Reads the previous visit timestamp from the cache and updates it with the current time
130
131
131
-
## Running the Application
132
+
## Running the application
132
133
133
134
1. Ensure you're authenticated:
134
135
135
136
```bash
136
137
az login
137
138
```
138
139
139
-
1. Update the Redis endpoint in `appsettings.Development.json`
140
+
1. Update the Redis endpoint in `appsettings.Development.json`.
140
141
141
142
1. Run the application:
142
143
143
144
```bash
144
145
dotnet run
145
146
```
146
147
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.
148
149
149
-
## Expected Output
150
+
## Expected output
150
151
151
152
When invoking the `GET /Sample` endpoint:
152
153
@@ -163,32 +164,32 @@ Previous visit was at: 2026-01-30T14:23:45
163
164
(Returns the ISO 8601 formatted timestamp of the previous request)
Handled GET request. Previous visit time: 2026-01-30T14:23:45
171
172
```
172
173
173
-
## Key Implementation Details
174
+
## Key implementation details
174
175
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.
176
177
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.
178
179
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.
180
181
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.
182
183
183
184
## Troubleshooting
184
185
185
186
| Issue | Resolution |
186
187
| ------- | ------------ |
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. |
188
189
|`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. |
-[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