Skip to content

Latest commit

 

History

History

dotnet-core

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 Core
Learn how to incorporate Azure Cache for Redis into a C# .NET Core console app using the StackExchange.Redis Redis client.
azure
dotnet
azure-cache-redis

Quickstart: Use Azure Cache for Redis in .NET Core

See the accompanying article on the documentation site for details, including best practices and how to create the sample code from scratch.

Prerequisites

Run the sample

Download the sample code to your development PC.

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 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 developer CLI. Bring up the Command Prompt. Run

azd auth login

For more information on signing into Azure with Azure developer CLI, see azd auth login

2. 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

3. In your command window, change directories to the folder containing this sample.

4. Execute the following command to restore the packages:

dotnet restore

5. Revise appsettings.json to specify Azure Redis host name

{
  "RedisHostName": "<your_redis_name>.redis.cache.windows.net"
}

6. Execute the following command in your command window to build the app:

dotnet build

Then run the app with the following command:

dotnet run

References