This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
In this quickstart, you incorporate Azure Cache for Redis into a .NET Core app to have access to a secure, dedicated cache that is accessible from any application within Azure. You specifically use theStackExchange.Redis client with C# code in a .NET Core console app.
Clone the repohttps://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/dotnet-core on GitHub.
To create an Azure Managed Redis instance, sign in to the Azure portal and selectCreate a resource.
On theCreate a resource page, typeAzure Managed Redis in the search box.
Select theAzure Managed Redis tile and selectCreate.
On theCreate Azure Managed Redis instance pane, configure the settings for a new cache on theBasics tab.
| Setting | Choose a value | Description |
|---|---|---|
| Subscription | Drop down and select your subscription. | The subscription under which to create this new Azure Managed Redis instance. |
| Resource group | Drop down and select a resource group, or selectCreate new and enter a new resource group name. | Name for the resource group in which to create your cache and other resources. By putting all your app resources in one resource group, you can easily manage or delete them together. |
| Name | Enter a name that is unique in the region. | The cache name must be a string between 1 and 63 characters whencombined with the cache's region name that contain only numbers, letters, or hyphens. (If the cache name is fewer than 45 characters long it should work in all currently available regions.) The name must start and end with a number or letter, and can't contain consecutive hyphens. Your cache instance'shost name is\<DNS name\>.\<Azure region\>.redis.azure.net. |
| Region | Drop down and select a location. | Azure Managed Redis is available in selected Azure regions. |
| Data tier | Select eitherIn-memory for high-performance, orFlash for lower performance caches | The in-memory tiers includeBalanced,Memory Optimized, andCompute Optimized. Use theFlash tier to use in-memory (RAM) and on-disk (SSD) data storage. |
| Cache Size | Drop down and select a size. | Cache size depends on tier. The smallest size is a Balanced tier. The size largest in-memory tier is a Memory Optimized tier. |
| Performance | Drop down and select a performance preference. | Performance depends on the number of vCPUs. Number of vCPU varies with tier. Compute Optimized has the most vCPUs. |
For guidance on choosing the right performance tier, seeChoosing the right tier.
Important
All in-memory tiers that use over 235 GB of storage are in Public Preview, including Memory Optimized M350 and higher; Balanced B350 and higher; and Compute Optimized X350 and higher. All these tiers and higher are in Public Preview.
All Flash Optimized tiers are in Public Preview.
SelectNext: Networking , and select either aDisable public access and use private access orEnable public access from all networks.
SelectNext: Active geo-replication. To use active geo-replication it must be enabled during provisioning. Caches without active geo-replication can't be added to or join active geo-replication groups later. For more information, seeConfigure active geo-replication for Azure Managed Redis instances.
Select theNext: Advanced tab.
Configure anyRedis modules you want to add to the instance.
SetClustering policy:
For more information on choosingClustering policy, seeCluster policy.
By default, for a new managed cache:
Important
You can't change modules after you create a cache instance. Modules must be enabled at the time you create an Azure Managed Redis instance. There is no option to enable the configuration of a module after you create a cache.
Important
For optimal security, we recommend that you use Microsoft Entra ID with managed identities to authorize requests against your cache if possible. Authorization by using Microsoft Entra ID and managed identities provides superior security and ease of use over shared access key authorization. For more information about using managed identities with your cache, seeUse Microsoft Entra ID for cache authentication.
Important
You can't change the clustering policy of an Azure Managed Redis instance after you create it. If you're usingRediSearch, the Enterprise cluster policy is required, andNoEviction is the only eviction policy supported.
Important
If you're using this cache instance in a geo-replication group, eviction policies cannot be changed after the instance is created. Be sure to know the eviction policies of your primary nodes before you create the cache. For more information on active geo-replication, seeActive geo-replication prerequisites.
SelectNext: Tags and skip.
SelectNext: Review + create.
Review the settings and selectCreate.
It takes several minutes for the Redis instance to create. You can monitor progress on the Azure Managed RedisOverview page. WhenStatus shows asRunning, the cache is ready to use.
In theAzure portal, search for and selectAzure Cache for Redis.
On theAzure Cache for Redis page, selectCreate >Azure Cache for Redis.
On theBasics tab of theNew Redis Cache page, configure the following settings:

Select theNetworking tab, or selectNext: Networking.
On theNetworking tab, select a connectivity method to use for the cache.Private Endpoint is recommended for security. If you selectPrivate Endpoint, selectAdd private endpoint and create the private endpoint.
Select theAdvanced tab, or selectNext: Advanced.
On theAdvanced pane, configure the following options:
The following image shows theAdvanced tab for the Standard SKU.

Important
Use Microsoft Entra ID with managed identities to authorize requests against your cache if possible. Authorization using Microsoft Entra ID and managed identity provides better security and is easier to use than shared access key authorization. For more information about using managed identities with your cache, seeUse Microsoft Entra ID for cache authentication.
Optionally, select theTags tab or selectNext: Tags, and enter tag names and values to categorize your cache resources.
SelectReview + create, and once validation passes, selectCreate.
The new cache deployment takes several minutes. You can monitor deployment progress on the portal Azure Cache for Redis page. When the cacheStatus displaysRunning, the cache is ready to use.
Azure Managed Redis caches have Microsoft Entra Authentication enabled by default.
In the Azure portal, select the cache where you'd like to use Microsoft Entra token-based authentication.
SelectAuthentication from the Resource menu.
SelectSelect member and enter the name of a valid user. The user you enter is automatically assignedData Owner Access Policy by default when you selectSave. You can also enter a managed identity or service principal to connect to your cache instance.

For information on using Microsoft Entra ID with Azure CLI, see thereference pages for identity.
TheAzure.StackExchange.Redis library contains the Microsoft Entra ID authentication method for connecting to Azure Redis services using Microsoft Entra ID. It's applicable to all Azure Cache for Redis, Azure Cache for Redis Enterprise, and Azure Managed Redis.
dotnet add package Microsoft.Azure.StackExchangeRedisInclude the libraries in your code
using Azure.Identity;using StackExchange.RedisUsing 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.
var configurationOptions = await ConfigurationOptions.Parse($"{_redisHostName}").ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential());ConnectionMultiplexer _newConnection = await ConnectionMultiplexer.ConnectAsync(configurationOptions);IDatabase Database = _newConnection.GetDatabase();Edit theWeb.config file. Then add the following content:
"_redisHostName":"<cache-hostname>"Replace<cache-hostname> with your cache host name as it appears in the Overview section of the Resource menu in the Azure portal.
For example, with Azure Managed Redis or the Enterprise tiers:my-redis.eastus.azure.net:10000
Save the file.
For more information, seeStackExchange.Redis and the code in aGitHub repo.
Edit theappsettings.json file. Then add the following content:
"_redisHostName":"<cache-hostname>"Replace<cache-hostname> with your cache host name as it appears in the Overview section of the Resource menu in the Azure portal.
For example, with Azure Cache for Redis:my-redis.eastus.azure.net:6380
Save the file.
For more information, seeStackExchange.Redis and the code in aGitHub repo.
If you opened any files, save them, and build the app with the following command:
dotnet buildTo test serialization of .NET objects, run the app with the following command:
dotnet run
If you want to continue to use the resources you created in this article, keep the resource group.
Otherwise, if you're finished with the resources, you can delete the Azure resource group that you created to avoid charges.
Important
Deleting a resource group is irreversible. When you delete a resource group, all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources inside an existing resource group that contains resources you want to keep, you can delete each resource individually instead of deleting the resource group.
Sign in to theAzure portal, and then selectResource groups.
Select the resource group you want to delete.
If there are many resource groups, use theFilter for any field... box, type the name of your resource group you created for this article. Select the resource group in the results list.

SelectDelete resource group.
You're asked to confirm the deletion of the resource group. Type the name of your resource group to confirm, and then selectDelete.

After a few moments, the resource group and all of its resources are deleted.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?