page_type | languages | name | description | products | ||||
---|---|---|---|---|---|---|---|---|
sample |
|
Quickstart: Use Azure Cache for Redis in .NET Framework |
Learn how to incorporate Azure Cache for Redis into a C# .NET Framework console app using the StackExchange.Redis Redis client. |
|
This sample shows you how to incorporate Azure Cache for Redis into a C# .NET Framework console app using the StackExchange.Redis Redis client. See the accompanying article on the documentation site for details, including best practices and how to create the sample code from scratch.
- Azure subscription - create one for free
- Azure Cache for Redis cache - create one
- Visual Studio 2019
- .NET Framework 4 or higher, which is required by the StackExchange.Redis client.
- Azure CLI
This sample uses Microsoft Entra ID to authenticate connections to an Azure Cache for Redis resource.
The following line of code in Redistest/RedisConnection.cs
obtains the default credential from your local machine or an Azure resource as the identity for authentication and authorization.
var configurationOptions = await ConfigurationOptions.Parse($"{_redisHostName}:6380").ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());
One of the common way for signing into to your Azure account is to use the Azure CLI. Bring up the Command Prompt. Run
az login
For other methods of sign into Azure with Azure CLI, such as using a Service Principal, see Sign into Azure with Azure CLI
Edit the App.config file and add the following contents:
<appSettings>
<add key="RedisHostName" value="<cache-name>.redis.cache.windows.net"/>
</appSettings>
Follow instruction at Use Microsoft Entra ID for cache authentication
Download the sample code to your development PC.
In Visual Studio, click File > Open > Project/Solution.
Navigate to the folder containing this sample, and select the solution file contained within it.
If the CacheSecrets.config file is not located at C:\AppSecrets\CacheSecrets.config, open your App.config file and update the appSettings
file
attribute to the correct path.
Press Ctrl+F5 to build and run the console app.