You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/redis/entra-for-authentication.md
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,6 +66,16 @@ If you have a cache where you use access keys, and you want to disable access ke
66
66
67
67
Because most Azure Managed Redis clients assume that a password and access key are used for authentication, you likely need to update your client workflow to support authentication by using Microsoft Entra. In this section, you learn how to configure your client applications to connect to Azure Managed Redis by using a Microsoft Entra token.
68
68
69
+
### Add users or System principal to your cache
70
+
71
+
1. Connect to your cache in the Azure portal
72
+
73
+
1. On the Resource menu, select **Authentication**.
74
+
1. On the **Microsoft Entra Authentication** tab, select **User or service principal** and then **+ Select member**.
75
+
1. Type the name of the user who you want to run the program. Select a user from the list and then **Select**. The user is added to the list of Redis users.
76
+
1.
77
+
:::image type="content" source="media/entra-for-authentication/redis-add-user.png" alt-text="Screenshot of the authentication tab in a Redis cache on the Azure portal.":::
78
+
69
79
### Microsoft Entra client workflow
70
80
71
81
1. Configure your client application to acquire a Microsoft Entra token for scope, `https://redis.azure.com/.default` or `acca5fbb-b7e4-4009-81f1-37e38fd66d78/.default`, by using the [Microsoft Authentication Library (MSAL)](/azure/active-directory/develop/msal-overview).
In this article, you learn how to use a Azure Redis cache with the Go language.
20
-
21
-
<!-- ## Skip to the code on GitHub
22
-
23
-
If you want to skip straight to the code, see the [Go quickstart](https://github.com/Azure-Samples/azure-redis-cache-go-quickstart/) on GitHub.
24
-
25
-
We are breaking the connection to this. -->
19
+
In this article, you learn how to use a Azure Redis cache with the Go language and connect using Microsoft Entra ID.
26
20
27
21
## Prerequisites
28
22
29
23
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
30
-
-[Go](https://go.dev/doc/install) (preferably version 1.13 or above)
24
+
-[Go](https://go.dev/doc/install)
25
+
- You must also add two imports from Redis to your project and add them to your development environment.
26
+
-`entraid "github.com/redis/go-redis-entraid"`
27
+
-`"github.com/redis/go-redis/v9"`
31
28
32
29
## Create an Azure Managed Redis instance
33
30
34
-
First you must create a cache. You can create a cache using Azure Managed Redis or Azure Cache for Redis using the Azure portal.
31
+
First you must create a cache. You can create a cache using Azure Managed Redis or Azure Cache for Redis using the Azure portal. In this Quickstart, we use Azure Managed Redis.
32
+
33
+
When you create the cache you should create it Microsoft Entra ID is enabled by default. Your cache must also use public endpoint for this QuickStart.
34
+
35
+
To create a chace with the portal, follow one of these procedure
35
36
36
-
When you create the cache you should create it with Access keys enabled. Microsoft Entra ID is enabled by default. Your cache must also use public endpoint for this Quickstart.
In the next section, we test the connection using the Redis command "ping" that returns the "pong" string.
50
86
51
-
## Code set a key, get a key
87
+
```go
88
+
// Ping the Redis server to test the connection
89
+
pong, err:= client.Ping(ctx).Result()
90
+
if err != nil {
91
+
log.Fatal("Failed to connect to Redis:", err)
92
+
}
93
+
fmt.Println("Ping returned: ", pong)
94
+
```
52
95
53
-
<!-- simple set key, get value -->
96
+
## Code set a key, get a key
54
97
55
-
<!-- clean up resources include -->
98
+
In this section, we show a basic `set` and `get` sequence to demonstrte using the Redis cache.
99
+
100
+
```go
101
+
// Do something with Redis and a key-value pair
102
+
result, err:= client.Set(ctx, "Message", "Hello, The cache is working with Go!", 0).Result()
103
+
if err != nil {
104
+
log.Fatal("SET Message failed:", err)
105
+
}
106
+
fmt.Println("SET Message succeeded:", result)
107
+
108
+
value, err:= client.Get(ctx, "Message").Result()
109
+
if err != nil {
110
+
if err == redis.Nil {
111
+
fmt.Println("GET Message returned: key does not exist")
112
+
} else {
113
+
log.Fatal("GET Message failed:", err)
114
+
}
115
+
} else {
116
+
fmt.Println("GET Message returned:", value)
117
+
}
118
+
119
+
}
120
+
121
+
```
122
+
123
+
Before you can run this code, you must add yourself as a Redis user. And you must also authorize your connection to Azure from the command line using the Azure command line or Azure developer command line (azd).
124
+
125
+
You should also [add any user](entra-for-authentication.md)[Add users or System principal to your cache](entra-for-authentication.md#add-users-or-system-principal-to-your-cache) who might run the program as a user on the Redis cache.
126
+
127
+
The result looks like this:
128
+
129
+
```
130
+
Ping returned: PONG
131
+
SET Message succeeded: OK
132
+
GET Message returned: Hello, The cache is working with Go!
133
+
```
134
+
You can see this code sample in its entirety.
135
+
136
+
```go
137
+
package main
138
+
139
+
import (
140
+
"context"
141
+
"crypto/tls"
142
+
"fmt"
143
+
"log"
144
+
"time"
145
+
146
+
entraid "github.com/redis/go-redis-entraid"
147
+
"github.com/redis/go-redis/v9"
148
+
)
149
+
150
+
funcmain() {
151
+
ctx:= context.Background()
152
+
153
+
// Set your Redis host (hostname:port)
154
+
redisHost:="<host >:<public port number>""
155
+
156
+
// Create a credentials provider using DefaultAzureCredential
0 commit comments