Skip to content

Commit 4d55366

Browse files
committed
Add new tutorial for AppService with Entra auth
1 parent e3a0690 commit 4d55366

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
title: 'Tutorial: Get started connecting an App Service hosted web app to Azure Cache for Redis'
3+
description: In this tutorial, you learn how to connect your App Service-hosted web application to an Azure Cache for Redis instance.
4+
author: flang-msft
5+
6+
ms.author: franlanglois
7+
ms.service: cache
8+
ms.topic: tutorial
9+
ms.date: 04/15/2024
10+
#CustomerIntent: As a developer, I want to see how to use a Azure Cache for Redis instance with an Azure App Service.
11+
12+
---
13+
14+
# Tutorial: Connect to Azure Cache for Redis from your we application hosted on Azure App Service
15+
16+
In this tutorial, you will start with a basic ASP.NET Core Web App and then add code snippet to connect to Azure Cache for Redis using a system assigned managed identity.
17+
18+
## Prerequisites
19+
20+
- An Azure subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
21+
- Visual Studio 2022 with the ASP.NET and web development workload.
22+
If you have already installed Visual Studio 2022:
23+
24+
Install the latest updates in Visual Studio by selecting Help > Check for Updates.
25+
Add the workload by selecting Tools > Get Tools and Features.
26+
27+
28+
## Set up an Azure Cache for Redis instance
29+
30+
1. Create a new Azure Cache for Redis instance by using the Azure portal or your preferred CLI tool. Use the [quickstart guide](quickstart-create-redis.md) to get started.
31+
32+
For this tutorial, use a Standard C1 cache.
33+
:::image type="content" source="media/cache-tutorial-aks-get-started/cache-new-instance.png" alt-text="Screenshot of creating a Standard C1 cache in the Azure portal":::
34+
35+
1. On the **Advanced** tab, enable Microsoft Entra Authentication.
36+
37+
1. Follow the steps through to create the cache.
38+
39+
1. Once your cache is created, go to the **Data Access Configuration** and click on Add > New Redis User.
40+
41+
1. Choose Data Contributor Access Policy and click Next. Under the "Assign access to" options, choose "Managed Identity" and click "Select member"
42+
43+
1. Choose your subscription and select App Service in "Managed Identity" dropdown.
44+
45+
1. Choose the user assigned managed identity for your App Service in the "Select" box and click the "Select" button.
46+
47+
1. Click the "Next: Review and assign" button followed by "Assign" button on next page.
48+
49+
## Create your web application and publish to Azure App Service
50+
51+
Follow the steps described in the [Azure App Service tutorial](https://learn.microsoft.com/en-us/azure/app-service/quickstart-dotnetcore?tabs=net70&pivots=development-environment-vs#1-create-an-aspnet-web-app) to create and publish your first Azure App Service.
52+
53+
### Update the web application to use Azure Cache for Redis
54+
55+
1. Open your web application in Visual Studio and right click on the project. Click on the Manage NuGet Packages. Browse and install the latest version of Microsoft.Azure.StackExchangeRedis.
56+
57+
1. Open the Index.cshtml file and append the following code to a button which will write data to your Azure Cache for Redis instance
58+
59+
```html
60+
<div class="text-center">
61+
<button onclick="UpdateTimeStamp()">Update timestamp</button>
62+
<input type="text" id="timestampTextBox" style="min-width:30%" />
63+
</div>
64+
65+
<script>
66+
function UpdateTimeStamp() {
67+
$.ajax({
68+
url: '@Url.Action("UpdateTimeStamp", "Home")',
69+
type: 'GET',
70+
success: function (data) {
71+
$('#timestampTextBox').val(data);
72+
}
73+
});
74+
}
75+
</script>
76+
```
77+
78+
1. Open the HomeController.cs file and add the following code to handle the click event for the new button you just added.
79+
80+
```CSharp
81+
[HttpGet]
82+
public async Task<IActionResult> UpdateTimeStamp()
83+
{
84+
await _redisDB.StringSetAsync(key, DateTime.UtcNow.ToString("s"));
85+
return Ok("Last timestamp: " + (await _redisDB.StringGetAsync(key)).ToString());
86+
}
87+
```
88+
89+
1. Open the Program.cs file and the following code snippet to instantiate a connection to your Azure Cache for Redis Instance.
90+
Note that
91+
- redisHostName is the hostname for the Azure Cache for Redis instance that you created earlier
92+
- webAppObjectId is the object (Principal) Id for your Azure App Service system assigned managed identity.
93+
94+
```CSharp
95+
const string redisHostName = "cachename.redis.cache.windows.net";
96+
const string webAppObjectId = "someguid";
97+
98+
var configOptions = await ConfigurationOptions.Parse($"{redisHostName}:6380").ConfigureForAzureWithSystemAssignedManagedIdentityAsync(webAppObjectId);
99+
100+
var redisConnection = await ConnectionMultiplexer.ConnectAsync(configOptions);
101+
var redisDB = redisConnection.GetDatabase();
102+
103+
builder.Services.AddSingleton(redisDB);
104+
105+
var app = builder.Build();
106+
```
107+
108+
109+
## Clean up your deployment
110+
111+
To clean up your cluster, run the following commands:
112+
113+
```bash
114+
kubectl delete deployment azure-vote-front
115+
kubectl delete service azure-vote-front
116+
```
117+
118+
[!INCLUDE [cache-delete-resource-group](includes/cache-delete-resource-group.md)]
119+
120+
## Related content
121+
122+
- [Quickstart: Deploy an Azure Kubernetes Service (AKS) cluster using the Azure portal](/azure/aks/learn/quick-kubernetes-deploy-portal)
123+
- [AKS sample voting application](https://github.com/Azure-Samples/azure-voting-app-redis/tree/master)

0 commit comments

Comments
 (0)