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
### Install the Library for using Entra ID Authentication
53
+
The [Azure.StackExchange.Redis](https://www.nuget.org/packages/Microsoft.Azure.StackExchangeRedis) library contains the Microsoft Entra ID authentication method for connecting to Azure Redis services using Entra ID. It is applicable to all Azure Cache for Redis, Azure Cache for Redis Enterprise, and Azure Managed Redis (Preview).
53
54
54
-
In your _appsettings.json_ file, add the following:
1. Replace `"your_Azure_Redis_hostname"` with your Azure Redis host name and port numbers. For example: `cache-name.region.redis.azure.net:10000` for Azure Managed Redis (preview), and `cache-name.redis.cache.windows.net:6380` for Azure Cache for Redis services.
63
-
64
-
1. Save the file.
65
-
66
59
---
67
60
68
-
## Connect to the cache with RedisConnection
69
-
70
-
In `RedisConnection.cs`, you see the `StackExchange.Redis` namespace has been added to the code. This is needed for the `RedisConnection` class.
71
-
72
-
```csharp
73
-
usingStackExchange.Redis;
61
+
## Connect to the cache using Entra ID
74
62
63
+
1. Include the libraries in your code
64
+
65
+
```
66
+
using Azure.Identity;
67
+
using StackExchange.Redis
75
68
```
76
69
77
-
The `RedisConnection` code ensures that there is always a healthy connection to the cache by managing the `ConnectionMultiplexer` instance from `StackExchange.Redis`. The `RedisConnection` class recreates the connection when a connection is lost and unable to reconnect automatically.
78
-
79
-
For more information, see [StackExchange.Redis](https://stackexchange.github.io/StackExchange.Redis/) and the code in a [GitHub repo](https://github.com/StackExchange/StackExchange.Redis).
In `program.cs`, you can see the following code for the `RunRedisCommandsAsync` method in the `Program` class for the console application:
86
-
70
+
1. Using the default Azure credentials to authenticate the client connection. This enables your code to use the signed-in user credential when running locally, and an Azure managed identity when running in Azure without code change.
Cache items can be stored and retrieved by using the `StringSetAsync` and `StringGetAsync` methods.
78
+
### To edit the *appsettings.json* file
128
79
129
-
In the example, you can see the `Message` key is set to value. The app updated that cached value. The app also executed the `PING` and command.
80
+
1. Edit the *Web.config* file. Then add the following content:
130
81
131
-
### Work with .NET objects in the cache
82
+
```json
83
+
"_redisHostName":"<cache-hostname>"
84
+
```
132
85
133
-
The Redis server stores most data as strings, but these strings can contain many types of data, including serialized binary data, which can be used when storing .NET objects in the cache.
86
+
1. Replace `<cache-hostname>` with your cache host name as it appears in the Overview blade of Azure Portal. For example, *my-redis.eastus.azure.net:10000*
134
87
135
-
Azure Cache for Redis can cache both .NET objects and primitive data types, but before a .NET object can be cached it must be serialized.
136
-
137
-
This .NET object serialization is the responsibility of the application developer, and gives the developer flexibility in the choice of the serializer.
88
+
1. Save the file.
138
89
139
-
The following `Employee` class was defined in _Program.cs_ so that the sample could also show how to get and set a serialized object:
90
+
For more information, see [StackExchange.Redis](https://stackexchange.github.io/StackExchange.Redis/) and the code in a [GitHub repo](https://github.com/StackExchange/StackExchange.Redis).
1. Edit the *App.config* file and add the following contents:
53
+
### Install the Library for using Entra ID Authentication
54
+
The [Azure.StackExchange.Redis](https://www.nuget.org/packages/Microsoft.Azure.StackExchangeRedis) library contains the Microsoft Entra ID authentication method for connecting to Azure Redis services using Entra ID. It is applicable to all Azure Cache for Redis, Azure Cache for Redis Enterprise, and Azure Managed Redis (Preview).
1. Replace `"your_Azure_Redis_hostname"` with your Azure Redis host name and port numbers. For example: `cache-name.eastus.redis.azure.net:10000` for Azure Cache for Redis Enterprise, and `cache-name.redis.cache.windows.net:6380` for Azure Cache for Redis services.
In this section, you prepare the console application to use the [StackExchange.Redis](https://github.com/StackExchange/StackExchange.Redis) client for .NET.
71
-
72
-
1. In Visual Studio, select **Tools** > **NuGet Package Manager** > **Package Manager Console**, and run the following command from the Package Manager Console window.
The value of the *CacheConnection* appSetting is used to reference the cache connection string from the Azure portal as the password parameter.
89
-
90
-
In `RedisConnection.cs`, you see the `StackExchange.Redis` namespace with the `using` keyword. This is needed for the `RedisConnection` class.
91
-
92
-
```csharp
93
-
usingStackExchange.Redis;
67
+
using Azure.Identity;
68
+
using StackExchange.Redis
94
69
```
95
70
96
-
The `RedisConnection` code ensures that there is always a healthy connection to the cache by managing the `ConnectionMultiplexer` instance from `StackExchange.Redis`. The `RedisConnection` class recreates the connection when a connection is lost and unable to reconnect automatically.
97
-
98
-
For more information, see [StackExchange.Redis](https://stackexchange.github.io/StackExchange.Redis/) and the code in a [GitHub repo](https://github.com/StackExchange/StackExchange.Redis).
In `program.cs`, you can see the following code for the `RunRedisCommandsAsync` method in the `Program` class for the console application:
105
-
71
+
1. Using the default Azure credentials to authenticate the client connection. This enables your code to use the signed-in user credential when running locally, and an Azure managed identity when running in Azure without code change.
Cache items can be stored and retrieved by using the `StringSetAsync` and `StringGetAsync` methods.
148
-
149
-
In the example, you can see the `Message` key is set to value. The app updated that cached value. The app also executed the `PING` and command.
150
-
151
-
### Work with .NET objects in the cache
152
-
153
-
The Redis server stores most data as strings, but these strings can contain many types of data, including serialized binary data, which can be used when storing .NET objects in the cache.
154
-
155
-
Azure Cache for Redis can cache both .NET objects and primitive data types, but before a .NET object can be cached it must be serialized.
79
+
### To edit the *CacheSecrets.config* file
156
80
157
-
This .NET object serialization is the responsibility of the application developer, and gives the developer flexibility in the choice of the serializer.
81
+
1. Create a file on your computer named *CacheSecrets.config*. Put it in a location where it won't be checked in with the source code of your sample application. For this quickstart, the *CacheSecrets.config* file is located at *C:\AppSecrets\CacheSecrets.config*.
158
82
159
-
One simple way to serialize objects is to use the `JsonConvert` serialization methods in `System.text.Json`.
83
+
1. Edit the *app.config* file. Then add the following content:
160
84
161
-
Add the `System.text.Json` namespace to Visual Studio:
<!-- :::image type="content" source="media/cache-dotnet-how-to-use-azure-redis-cache/cache-console-app-partial.png" alt-text="Screenshot that shows console app."::: -->
91
+
1. Replace `<cache-hostname>` with your cache host name as it appears in the Overview blade of Azure Portal. For example, *my-redis.eastus.azure.net:10000*
172
92
173
-
The following `Employee` class was defined in *Program.cs* so that the sample could also show how to get and set a serialized object:
93
+
1. Save the file.
94
+
95
+
For more information, see [StackExchange.Redis](https://stackexchange.github.io/StackExchange.Redis/) and the code in a [GitHub repo](https://github.com/StackExchange/StackExchange.Redis).
174
96
175
-
```csharp
176
-
class Employee
177
-
{
178
-
public string Id { get; set; }
179
-
public string Name { get; set; }
180
-
public int Age { get; set; }
181
-
182
-
public Employee(string employeeId, string name, int age)
0 commit comments