Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
page_type languages name description products
sample
csharp
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.
azure
dotnet
azure-cache-redis

Quickstart: Use Azure Cache for Redis in .NET Framework

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.

Prerequisites

Set up the working environment

1. Set up local credential for using Entra ID

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

2. Point to an Azure Cache for Redis instance from local configuration

Edit the App.config file and add the following contents:

<appSettings>
    <add key="RedisHostName" value="<cache-name>.redis.cache.windows.net"/>
</appSettings>

3. Add the permissions to allow the Entra ID to connect to the Azure Cache for Redis instance

Follow instruction at Use Microsoft Entra ID for cache authentication

Run the sample

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.

References