Skip to content

Commit b0c90ea

Browse files
authored
Merge pull request #311155 from flang-msft/fxl---dotnet-quickstart
fxl---dotnet quickstart redux
2 parents d2b9222 + 3c8247b commit b0c90ea

16 files changed

Lines changed: 402 additions & 677 deletions

articles/azure-cache-for-redis/cache-best-practices-client-libraries.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ For client library-specific guidance and best practices, see the following links
5252

5353
Besides the reference documentation, you can use the following tutorials to get started with Azure Redis using different languages and cache clients:
5454

55-
- [Code a .NET Framework app](../redis/dotnet-how-to-use-azure-redis-cache.md)
56-
- [Code a .NET Core app](../redis/dotnet-core-quickstart.md)
57-
- [Code an ASP.NET web app](../redis/web-app-cache-howto.md)
58-
- [Code an ASP.NET Core web app](../redis/web-app-aspnet-core-howto.md)
55+
<!-- - [Code a .NET app](../redis/dotnet.md) -->
56+
<!-- - [Code an ASP.NET app](../redis/aspnet.md) -->
5957
- [Code a Java app](../redis/java-get-started.md)
6058

6159
## Best practices for Redisson (Java)
@@ -74,8 +72,6 @@ Here are some recommended best practices for the [Redisson](https://redisson.pro
7472

7573
- Reset the idle connection timeout if necessary. Redisson has a default 10-second idle connection timeout, which can lead to more closing and reopening of connections than ideal.
7674

77-
- For information about using Redisson with Java EE JCache to store HTTP session state on an Azure Kubernetes Service (AKS) cluster, see [Using Azure Redis as session cache for WebSphere Liberty or Open Liberty](/azure/developer/java/ee/how-to-deploy-java-liberty-jcache).
78-
7975
- Use the following recommended baseline configuration for cluster mode, and modify it as needed.
8076

8177
```yml

articles/azure-cache-for-redis/cache-best-practices-development.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22
title: Best practices for development
33
description: Learn how to develop code for Azure Cache for Redis.
4-
5-
64
ms.topic: conceptual
75
ms.custom:
86
- ignite-2024

articles/azure-cache-for-redis/cache-development-faq.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ sections:
2222
answer: |
2323
There are several ways you can get started with Azure Cache for Redis.
2424
25-
* You can check out one of our tutorials available for [.NET](../redis/dotnet-how-to-use-azure-redis-cache.md), [ASP.NET](../redis/web-app-cache-howto.md), [Java](../redis/java-get-started.md), [Node.js](../redis/nodejs-get-started.md), and [Python](../redis/python-get-started.md).
25+
* You can check out one of our tutorials available for [.NET](../redis/dotnet.md), [ASP.NET](../redis/aspnet.md), [Java](../redis/java-get-started.md), [Node.js](../redis/nodejs-get-started.md), and [Python](../redis/python-get-started.md).
2626
* You can watch [How to Build High-Performance Apps Using Microsoft Azure Cache for Redis](https://azure.microsoft.com/resources/videos).
2727
* You can check out the client documentation for the example clients that match the development language you use in your project. There are many Redis clients that can be used with Azure Cache for Redis. For a list of Redis clients, see [https://redis.io/clients](https://redis.io/clients).
2828
@@ -65,7 +65,7 @@ sections:
6565
- question: |
6666
What Azure Cache for Redis clients can I use?
6767
answer: |
68-
One of the great things about Redis is that there are many clients supporting many different development languages. For a current list of clients, see [Redis clients](https://redis.io/docs/latest/operate/rs/databases/connect/supported-clients-browsers/). For tutorials that cover several different languages and clients, see [How to use Azure Cache for Redis](../redis/dotnet-how-to-use-azure-redis-cache.md).
68+
One of the great things about Redis is that there are many clients supporting many different development languages. For a current list of clients, see [Redis clients](https://redis.io/docs/latest/operate/rs/databases/connect/supported-clients-browsers/).
6969
7070
[!INCLUDE [redis-cache-create](includes/redis-cache-access-keys.md)]
7171
@@ -108,7 +108,7 @@ sections:
108108
answer: |
109109
Microsoft Azure Cache for Redis is based on the popular open-source in-memory data store, Redis. You can use a wide variety of [Redis clients](https://redis.io/docs/latest/operate/rs/databases/connect/supported-clients-browsers/) for many programming languages. Each client has its own API that makes calls to the Azure Cache for Redis instance using [Redis commands](https://redis.io/commands).
110110
111-
Because each client is different, you can't find one centralized class reference on MSDN. Each client maintains its own reference documentation. Besides the reference documentation, there are several tutorials showing how to get started with Azure Cache for Redis using different languages and cache clients. To access these tutorials, see [How to use Azure Cache for Redis](../redis/dotnet-how-to-use-azure-redis-cache.md) and its sibling articles in the table of contents.
111+
Because each client is different, you can't find one centralized class reference on MSDN. Each client maintains its own reference documentation. Besides the reference documentation, there are several tutorials showing how to get started with Azure Cache for Redis using different languages and cache clients.
112112
113113
- question: |
114114
Can I use Azure Cache for Redis as a PHP session cache?

articles/azure-cache-for-redis/cache-overview.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ Azure Cache for Redis is continually expanding into new regions. To check the av
109109
## Related content
110110

111111
- [Create an open-source Redis cache](quickstart-create-redis.md)
112-
- [Use Azure Cache for Redis in an ASP.NET web app](../redis/web-app-cache-howto.md)
113-
- [Use Azure Cache for Redis in .NET Core](../redis/dotnet-core-quickstart.md)
114-
- [Use Azure Cache for Redis in .NET Framework](../redis/dotnet-how-to-use-azure-redis-cache.md)
115112
- [Use Azure Cache for Redis in Node.js](../redis/nodejs-get-started.md)
116113
- [Use Azure Cache for Redis in Java](../redis/java-get-started.md)
117114
- [Use Azure Cache for Redis in Python](../redis/python-get-started.md)

articles/redis/.openpublishing.redirection.redis.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,26 @@
7575
"source_path_from_root": "/articles/redis/tutorial-semantic-cache.md",
7676
"redirect_url": "/azure/redis/overview-vector-similarity",
7777
"redirect_document_id": false
78+
},
79+
{
80+
"source_path_from_root": "/articles/redis/dotnet-core-quickstart.md",
81+
"redirect_url": "/azure/redis/dotnet",
82+
"redirect_document_id": false
83+
},
84+
{
85+
"source_path_from_root": "/articles/redis/dotnet-how-to-use-azure-redis-cache.md",
86+
"redirect_url": "/azure/redis/dotnet",
87+
"redirect_document_id": false
88+
},
89+
{
90+
"source_path_from_root": "/articles/redis/web-app-aspnet-core-howto.md",
91+
"redirect_url": "/azure/redis/aspnet",
92+
"redirect_document_id": false
93+
},
94+
{
95+
"source_path_from_root": "/articles/redis/web-app-cache-howto.md",
96+
"redirect_url": "/azure/redis/aspnet",
97+
"redirect_document_id": false
7898
}
7999
]
80100
}

articles/redis/TOC.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ items:
2323
displayName: Resource Manager
2424
- name: Use a cache
2525
items:
26-
- name: ASP.NET app
27-
href: web-app-cache-howto.md
28-
- name: ASP.NET Core app
29-
href: web-app-aspnet-core-howto.md
30-
- name: .NET Core app
31-
href: dotnet-core-quickstart.md
3226
- name: .NET app
33-
href: dotnet-how-to-use-azure-redis-cache.md
27+
href: dotnet.md
28+
- name: ASP.NET app
29+
href: aspnet.md
3430
- name: Node.js app
3531
href: nodejs-get-started.md
3632
- name: Python app

articles/redis/aspnet.md

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
---
2+
title: Create an ASP.NET web app with an Azure Managed Redis cache
3+
description: In this quickstart, you learn how to create an ASP.NET Core web app with an Azure Managed Redis cache.
4+
ms.date: 01/30/2026
5+
ms.topic: quickstart
6+
ms.devlang: csharp
7+
zone_pivot_groups: redis-type
8+
appliesto:
9+
- ✅ Azure Managed Redis
10+
ai-usage: ai-assisted
11+
# Customer intent: As an ASP.NET developer, new to Azure Managed Redis, I want to create a new .NET app that uses Azure Managed Redis.
12+
---
13+
14+
# Azure Managed Redis sample - ASP.NET Core Web API
15+
16+
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.
17+
18+
The application is a minimal ASP.NET Core 8.0 Web API that:
19+
20+
1. Establishes a secure, authenticated connection to Azure Managed Redis at startup.
21+
1. Exposes a simple REST endpoint that reads and writes data to the cache.
22+
1. Demonstrates proper Redis connection lifecycle management by using dependency injection.
23+
24+
## Skip to the code on GitHub
25+
26+
Clone the [Microsoft.Azure.StackExchangeRedis](https://github.com/Azure/Microsoft.Azure.StackExchangeRedis/tree/main/sample.aspnet) repo on GitHub.
27+
28+
## Prerequisites
29+
30+
- [.NET 8.0 SDK](https://dotnet.microsoft.com/download/dotnet/8.0).
31+
- An **Azure Managed Redis** instance provisioned in your Azure subscription.
32+
- Your Azure user or service principal must be added as a Redis user on the cache. In the Azure portal, go to **Authentication** on the Resource menu, select **User or service principal**, and add your identity.
33+
- [Azure CLI](/cli/azure/install-azure-cli?view=azure-cli-latest) for local development authentication.
34+
35+
## Required NuGet Packages
36+
37+
| Package | Purpose |
38+
| --------- | --------- |
39+
| `Microsoft.Azure.StackExchangeRedis` | Extension methods for StackExchange.Redis that enable Microsoft Entra ID token-based authentication to Azure Managed Redis |
40+
| `StackExchange.Redis` | The underlying Redis client library for .NET |
41+
| `Azure.Identity` | Provides `DefaultAzureCredential` and other credential types for authenticating with Azure services |
42+
| `Swashbuckle.AspNetCore` | Swagger/OpenAPI support for API documentation and testing |
43+
44+
Install the primary package:
45+
46+
```bash
47+
dotnet add package Microsoft.Azure.StackExchangeRedis
48+
```
49+
50+
This package brings in `StackExchange.Redis` and `Azure.Identity` as dependencies.
51+
52+
## Configuration
53+
54+
The application reads the Redis endpoint from configuration. Update `appsettings.Development.json`:
55+
56+
```json
57+
{
58+
"Redis": {
59+
"Endpoint": "<your-redis-name>.<region>.redis.azure.net:10000"
60+
}
61+
}
62+
```
63+
64+
> [!NOTE]
65+
> Azure Managed Redis uses port `10000` by default. The endpoint format follows `<cache-name>.<region>.redis.azure.net:10000`.
66+
67+
## Authentication Flow
68+
69+
### Local Development
70+
71+
Before running the application locally, authenticate with Azure:
72+
73+
```bash
74+
az login
75+
```
76+
77+
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.
78+
79+
### Production environments
80+
81+
In Azure-hosted environments such as App Service, Container Apps, and AKS, `DefaultAzureCredential` uses:
82+
83+
- **Managed Identity** - system-assigned or user-assigned
84+
- **Workload Identity** - for Kubernetes scenarios
85+
- **Environment variables** - for service principal authentication
86+
87+
You don't need to change your code. The same `DefaultAzureCredential` seamlessly adapts to the environment.
88+
89+
## Architecture
90+
91+
### Redis service (`Services/Redis.cs`)
92+
93+
The `Redis` class manages the connection lifecycle:
94+
95+
```csharp
96+
var options = new ConfigurationOptions()
97+
{
98+
EndPoints = { endpoint },
99+
LoggerFactory = _loggerFactory,
100+
};
101+
102+
await options.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
103+
104+
_connection = await ConnectionMultiplexer.ConnectAsync(options);
105+
```
106+
107+
Key points:
108+
109+
- `ConfigureForAzureWithTokenCredentialAsync` is an extension method from `Microsoft.Azure.StackExchangeRedis` that sets up token-based authentication
110+
- `DefaultAzureCredential` automatically handles token acquisition and refresh
111+
- The app establishes the connection once at startup and shares it across requests
112+
113+
### Dependency injection (`Program.cs`)
114+
115+
The app registers the Redis service as a singleton and initializes it during startup:
116+
117+
```csharp
118+
builder.Services.AddSingleton<Redis>();
119+
120+
// Initialize Redis connection
121+
using (var scope = app.Services.CreateScope())
122+
{
123+
var redis = scope.ServiceProvider.GetRequiredService<Redis>();
124+
var endpoint = app.Configuration.GetValue<string>("Redis:Endpoint");
125+
await redis.ConnectAsync(endpoint);
126+
}
127+
```
128+
129+
### API Controller (`Controllers/SampleController.cs`)
130+
131+
The controller injects the `Redis` service and demonstrates basic cache operations:
132+
133+
- **GET `/Sample`**: Reads the previous visit timestamp from the cache and updates it with the current time
134+
135+
## Running the application
136+
137+
1. Ensure you're authenticated:
138+
139+
```bash
140+
az login
141+
```
142+
143+
1. Update the Redis endpoint in `appsettings.Development.json`.
144+
145+
1. Run the application:
146+
147+
```bash
148+
dotnet run
149+
```
150+
151+
1. Navigate to `https://localhost:<port>/swagger` to access the Swagger UI.
152+
153+
## Expected output
154+
155+
When invoking the `GET /Sample` endpoint:
156+
157+
**First request:**
158+
159+
```bash
160+
Previous visit was at:
161+
(Empty value since no previous visit exists)
162+
```
163+
164+
```bash
165+
**Subsequent requests:**
166+
Previous visit was at: 2026-01-30T14:23:45
167+
(Returns the ISO 8601 formatted timestamp of the previous request)
168+
```
169+
170+
The console logs display:
171+
172+
```bash
173+
info: Microsoft.Azure.StackExchangeRedis.Sample.AspNet.Controllers.SampleController
174+
Handled GET request. Previous visit time: 2026-01-30T14:23:45
175+
```
176+
177+
## Key implementation details
178+
179+
- **Token refresh**: The `Microsoft.Azure.StackExchangeRedis` library automatically refreshes tokens before they expire, so you don't need to handle refresh manually.
180+
181+
- **Connection resilience**: The `ConnectionMultiplexer` from StackExchange.Redis manages reconnection logic on its own.
182+
183+
- **Resource cleanup**: The `Redis` service implements `IDisposable` to properly close the connection when the application shuts down.
184+
185+
- **Logging integration**: The Redis client works with .NET's `ILoggerFactory` for unified logging output.
186+
187+
## Troubleshooting
188+
189+
| Issue | Resolution |
190+
| ------- | ------------ |
191+
| `No connection is available` | Verify the endpoint format and port (`10000`). Make sure the Redis instance is provisioned and accessible. |
192+
| `AuthenticationFailedException` | Run `az login` to refresh credentials. Verify your identity is added as a Redis user under **Authentication** on the Resource menu. |
193+
| `Unauthorized` | Ensure your Microsoft Entra ID identity is added as a Redis user on the Azure Managed Redis instance. For more information, see [Use Microsoft Entra ID for cache authentication](entra-for-authentication.md). |
194+
195+
## Related content
196+
197+
- [Microsoft Entra ID authentication for Azure Managed Redis](entra-for-authentication.md)
198+
- [DefaultAzureCredential overview](/dotnet/azure/sdk/authentication)

articles/redis/best-practices-client-libraries.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ Each client library maintains its own reference documentation. The libraries als
2727

2828
The following recommendations are based on popularity and whether there's an active online community to support and answer your questions. We only recommend using the latest available version, and upgrading regularly as new versions become available. These libraries are under active development and often release new versions with improvements to reliability and performance.
2929

30-
| **Client library** | **Language** | **GitHub** **repo** | **Documentation** |
31-
|---------------------------|--------------|---------------------------------------------------------------------|--------------------------------------------------------------------------------------|
32-
| StackExchange.Redis | C#/.NET | [StackExchange.Redis GitHub](https://github.com/StackExchange/StackExchange.Redis) | [StackExchange.Redis docs](https://stackexchange.github.io/StackExchange.Redis/) |
33-
| Lettuce | Java | [Lettuce GitHub](https://github.com/lettuce-io/) | [Lettuce documentation](https://lettuce.io/) |
34-
| Jedis | Java | [Jedis GitHub](https://github.com/redis/jedis) | [Jedis documentation](https://github.com/redis/jedis#readme) |
35-
| node_redis | Node.js | [node_redis GitHub](https://github.com/redis/node-redis) | [node_redis documentation](https://github.com/redis/node-redis#readme) |
36-
| ioredis | Node.js | [ioredis GitHub](https://github.com/luin/ioredis) | [ioredis API reference](https://ioredis.readthedocs.io/en/stable/API/) |
30+
| **Client library** | **Language** | **GitHub** **repo** | **Documentation** |
31+
|---------------------|--------------|------------------------------------------------------------------------------------|----------------------------------------------------------------------------------|
32+
| StackExchange.Redis | C#/.NET | [StackExchange.Redis GitHub](https://github.com/StackExchange/StackExchange.Redis) | [StackExchange.Redis docs](https://stackexchange.github.io/StackExchange.Redis/) |
33+
| Lettuce | Java | [Lettuce GitHub](https://github.com/lettuce-io/) | [Lettuce documentation](https://lettuce.io/) |
34+
| Jedis | Java | [Jedis GitHub](https://github.com/redis/jedis) | [Jedis documentation](https://github.com/redis/jedis#readme) |
35+
| node_redis | Node.js | [node_redis GitHub](https://github.com/redis/node-redis) | [node_redis documentation](https://github.com/redis/node-redis#readme) |
36+
| ioredis | Node.js | [ioredis GitHub](https://github.com/luin/ioredis) | [ioredis API reference](https://ioredis.readthedocs.io/en/stable/API/) |
3737

3838
> [!NOTE]
3939
> Your application can use any client library that's compatible with open-source Redis to connect to your Azure Managed Redis instance.
@@ -73,7 +73,6 @@ In Active-Active databases, multi-key write commands (`DEL`, `MSET`, `UNLINK`) c
7373

7474
Choose your programming language to get detailed best practices and connection guidance:
7575

76-
- [StackExchange.Redis (.NET) best practices](best-practices-connection.md#using-forcereconnect-with-stackexchangeredis)
7776
- [Lettuce (Java) best practices](https://github.com/Azure/AzureCacheForRedis/blob/main/Lettuce%20Best%20Practices.md)
7877
- [Jedis (Java) best practices](https://github.com/Azure/AzureCacheForRedis/blob/main/Redis-BestPractices-Java-Jedis.md)
7978
- [Node.js best practices](https://github.com/Azure/AzureCacheForRedis/blob/main/Redis-BestPractices-Node-js.md)
@@ -86,10 +85,8 @@ Besides the reference documentation, you can find tutorials showing how to get s
8685

8786
For more information on using some of these client libraries in tutorials, see the following articles:
8887

89-
- [Code a .NET Framework app](dotnet-how-to-use-azure-redis-cache.md)
90-
- [Code a .NET Core app](dotnet-core-quickstart.md)
91-
- [Code an ASP.NET web app](web-app-cache-howto.md)
92-
- [Code an ASP.NET Core web app](web-app-aspnet-core-howto.md)
88+
- [Code a .NET app](dotnet.md)
89+
- [Code an ASP.NET web app](aspnet.md)
9390
- [Code a Java app](java-get-started.md)
9491
- [Code a Node.js app](nodejs-get-started.md)
9592
- [Code a Python app](python-get-started.md)

0 commit comments

Comments
 (0)