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
In this article, you incorporate Azure Cache for Redis into a Python app to have access to a secure, dedicated cache that is accessible from any application within Azure.
@@ -39,18 +40,25 @@ The following example used `pip3` for Python 3 to install `redis-py` on Windows
<!-- ## Enable Microsoft EntraID and add a User or Service Principal -->
46
+
<!--
47
+
Fran, we probably need an include file on enabling EntraID
48
+
Blah blah blah, do the steps listed [here](cache-azure-active-directory-for-authentication)
49
+
-->
45
50
46
51
## Install the Microsoft Authentication Library
47
-
The [Microsoft Authentication Library (MSAL)](../../entra/identity-platform/msal-overview) allows you to acquire security tokens from Microsoft identity to authenticate users. There's a [Python Azure identity client library](../../python/api/overview/azure/identity-readme) available that uses MSAL to provide token authentication support. Install this library using `pip`:
52
+
The [Microsoft Authentication Library (MSAL)](../../entra/identity-platform/msal-overview) allows you to acquire security tokens from Microsoft identity to authenticate users.
53
+
54
+
There's a [Python Azure identity client library](../../python/api/overview/azure/identity-readme) available that uses MSAL to provide token authentication support. Install this library using `pip`:
48
55
49
56
```python
50
57
pip install azure-identity
51
58
```
52
59
53
60
## Create a sample python app
61
+
54
62
Create a new text file, add the following script, and save the file as `PythonApplication1.py`. Replace `<Your Host Name>` with the value from your Azure Cache for Redis instance. Your host name is of the form `<DNS name>.redis.cache.windows.net`. Replace `<Your Username>` with the values from your Microsoft EntraID user.
55
63
56
64
```python
@@ -95,7 +103,8 @@ Run `PythonApplication1.py` with Python. You should see results like the followi
95
103
:::image type="content" source="media/cache-python-get-started/cache-python-completed.png" alt-text="Screenshot of a terminal showing a Python script to test cache access.":::
96
104
97
105
## Create a sample python app with reauthentication
98
-
Microsoft EntraID access tokens have limited lifespans, [averaging 75 minutes](../../entra/identity-platform/configurable-token-lifetimes#token-lifetime-policies-for-access-saml-and-id-tokens). In order to maintain a connection to your cache, you need to refresh the token. This example demonstrates how to do this using Python.
106
+
107
+
Microsoft EntraID access tokens have limited lifespans, [averaging 75 minutes](/azure/entra/identity-platform/configurable-token-lifetimes#token-lifetime-policies-for-access-saml-and-id-tokens). In order to maintain a connection to your cache, you need to refresh the token. This example demonstrates how to do this using Python.
99
108
100
109
Create a new text file, add the following script, and save the file as `PythonApplication2.py`. Replace `<Your Host Name>` with the value from your Azure Cache for Redis instance. Your host name is of the form `<DNS name>.redis.cache.windows.net`. Replace `<Your Username>` with the values from your Microsoft EntraID user.
101
110
@@ -171,11 +180,11 @@ Run `PythonApplication2.py` with Python. You should see results like the followi
171
180
Unlike the first example, If your token expires, this example automatically refreshes it.
## Read and write to the cache from the command line
177
187
178
-
179
188
Run [Python from the command line](https://docs.python.org/3/faq/windows.html#id2) to test your cache. First, initiate the python interpreter in your command line by typing `py`, and then use the following code. Replace `<Your Host Name>` and `<Your Access Key>` with the values from your Azure Cache for Redis instance. Your host name is of the form `<DNS name>.redis.cache.windows.net`.
### Enable Microsoft Entra ID authentication on your cache
10
+
11
+
1. In the Azure portal, select the Azure Cache for Redis instance where you'd like to configure Microsoft Entra token-based authentication.
12
+
13
+
1. Select **Authentication** from the Resource menu.
14
+
15
+
1. In the working pane, select **Enable Microsoft Entra Authentication**.
16
+
17
+
1. Select **Enable Microsoft Entra Authentication**, and enter the name of a valid user. The user you enter is automatically assigned _Data Owner Access Policy_ by default when you select **Save**. You can also enter a managed identity or service principal to connect to your cache instance.
18
+
19
+
:::image type="content" source="media/cache-entra-access/cache-enable-microsoft-entra.png" alt-text="Screenshot showing authentication selected in the resource menu and the enable Microsoft Entra authentication checked.":::
20
+
21
+
1. A popup dialog box displays asking if you want to update your configuration, and informing you that it takes several minutes. Select **Yes.**
22
+
23
+
> [!IMPORTANT]
24
+
> Once the enable operation is complete, the nodes in your cache instance reboots to load the new configuration. We recommend performing this operation during your maintenance window or outside your peak business hours. The operation can take up to 30 minutes.
25
+
26
+
For information on using Microsoft Entra ID with Azure CLI, see the [references pages for identity](/cli/azure/redis/identity).
0 commit comments