Quickstart: Use Azure Cache for Redis with an ASP.NET Core web app

In this quickstart, you incorporate Azure Cache for Redis into an ASP.NET Core web application that connects to Azure Cache for Redis to store and retrieve data from the cache.

Skip to the code on GitHub

Clone the repo https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/aspnet-core on GitHub.


Create a cache

  1. To create a cache, sign in to the Azure portal and select Create a resource.

    Create a resource is highlighted in the left navigation pane.

  2. On the New page, select Databases and then select Azure Cache for Redis.

    On New, Databases is highlighted, and Azure Cache for Redis is highlighted.

  3. On the New Redis Cache page, configure the settings for your new cache.

    Setting Choose a value Description
    Subscription Drop down and select your subscription. The subscription under which to create this new Azure Cache for Redis instance.
    Resource group Drop down and select a resource group, or select Create 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.
    DNS name Enter a unique name. The cache name must be a string between 1 and 63 characters that contain only numbers, letters, or hyphens. The name must start and end with a number or letter, and can't contain consecutive hyphens. Your cache instance's host name will be <DNS name>.redis.cache.windows.net.
    Location Drop down and select a location. Select a region near other services that will use your cache.
    Cache type Drop down and select a tier. The tier determines the size, performance, and features that are available for the cache. For more information, see Azure Cache for Redis Overview.
  4. Select the Networking tab or select the Networking button at the bottom of the page.

  5. In the Networking tab, select your connectivity method.

  6. Select the Next: Advanced tab or select the Next: Advanced button on the bottom of the page.

  7. In the Advanced tab for a basic or standard cache instance, select the enable toggle if you want to enable a non-TLS port. You can also select which Redis version you would like use, either 4 or 6.

    Redis version 4 or 6.

  8. In the Advanced tab for premium cache instance, configure the settings for non-TLS port, clustering, and data persistence. You can also select which Redis version you would like use, either 4 or 6.

  9. Select the Next: Tags tab or select the Next: Tags button at the bottom of the page.

  10. Optionally, in the Tags tab, enter the name and value if you wish to categorize the resource.

  11. Select Review + create. You're taken to the Review + create tab where Azure validates your configuration.

  12. After the green Validation passed message appears, select Create.

It takes a while for the cache to create. You can monitor progress on the Azure Cache for Redis Overview page. When Status shows as Running, the cache is ready to use.

Retrieve host name, ports, and access keys from the Azure portal

To connect your Azure Cache for Redis server, the cache client needs the host name, ports, and a key for the cache. Some clients might refer to these items by slightly different names. You can get the host name, ports, and keys from the Azure portal.

  • To get the access keys, from your cache left navigation, select Access keys.

    Azure Cache for Redis keys

  • To get the host name and ports, from your cache left navigation, select Properties. The host name is of the form <DNS name>.redis.cache.windows.net.

    Azure Cache for Redis properties

Make a note of the HOST NAME and the Primary access key. You use these values later to construct the CacheConnection secret.

Add a local secret for the connection string

In your command window, execute the following command to store a new secret named CacheConnection, after replacing the placeholders, including angle brackets, for your cache name and primary access key:

dotnet user-secrets set CacheConnection "<cache name>.redis.cache.windows.net,abortConnect=false,ssl=true,allowAdmin=true,password=<primary-access-key>"

Connect to the cache with RedisConnection

The connection to your cache is managed by the RedisConnection class. The connection is made in this statement in HomeController.cs in the Controllers folder:

_redisConnection = await _redisConnectionFactory;

In RedisConnection.cs, you see the StackExchange.Redis namespace has been added to the code. This is needed for the RedisConnection class.

using StackExchange.Redis;

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.

For more information, see StackExchange.Redis and the code in a GitHub repo.

Layout views in the sample

The home page layout for this sample is stored in the _Layout.cshtml file. From this page, you start the actual cache testing by clicking the Azure Cache for Redis Test from this page.

  1. Open Views\Shared\_Layout.cshtml.

  2. You should see in <div class="navbar-header">:

    <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="RedisCache">Azure Cache for Redis Test</a>

screenshot of welcome page

Showing data from the cache

From the home page, you select Azure Cache for Redis Test to see the sample output.

  1. In Solution Explorer, expand the Views folder, and then right-click the Home folder.

  2. You should see this code in the RedisCache.cshtml file.

        ViewBag.Title = "Azure Cache for Redis Test";
    <br /><br />
    <table border="1" cellpadding="10">

Run the app locally

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

    dotnet build
  2. Then run the app with the following command:

    dotnet run
  3. Browse to https://localhost:5001 in your web browser.

  4. Select Azure Cache for Redis Test in the navigation bar of the web page to test cache access.

Screenshot of simple test completed local

Clean up resources

If you continue to use this quickstart, you can keep the resources you created and reuse them.

Otherwise, if you're finished with the quickstart sample application, you can delete the Azure resources that you created in this quickstart to avoid charges.


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 for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually on the left instead of deleting the resource group.

To delete a resource group

  1. Sign in to the Azure portal, and then select Resource groups.

  2. In the Filter by name... box, type the name of your resource group. The instructions for this article used a resource group named TestResources. On your resource group, in the results list, select ..., and then select Delete resource group.


  3. You're asked to confirm the deletion of the resource group. Type the name of your resource group to confirm, and then select Delete.

After a few moments, the resource group and all of its resources are deleted.

Next steps