Skip to content

Commit c70b375

Browse files
committed
more change
1 parent a9a8ffa commit c70b375

File tree

1 file changed

+87
-97
lines changed

1 file changed

+87
-97
lines changed

articles/app-service/tutorial-connect-msi-sql-database.md

Lines changed: 87 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ For guidance about using Azure Database for MySQL or Azure Database for PostgreS
5454
<a name='1-grant-database-access-to-azure-ad-user'></a>
5555
## Grant admin access to a Microsoft Entra user
5656

57-
Enable Microsoft Entra authentication to SQL Database by assigning a Microsoft Entra user as the admin of the Azure SQL server. This user might not be the same as the Microsoft account user for your Azure subscription. The Microsoft Entra admin must be a user that is created, imported, synced, or invited into Microsoft Entra ID.
57+
Enable Microsoft Entra authentication to the Azure SQL database by assigning a Microsoft Entra user as the admin of the Azure SQL server. The Microsoft Entra admin must be a user that is created, imported, synced, or invited into Microsoft Entra ID. This user might not be the same as the Microsoft account user for your Azure subscription.
5858

5959
- For more information on creating a Microsoft Entra user, see [Add or delete users using Microsoft Entra ID](/entra/fundamentals/how-to-create-delete-users).
6060
- For more information on allowed Microsoft Entra users for SQL Database, see [Microsoft Entra features and limitations in SQL Database](/azure/azure-sql/database/authentication-aad-overview#limitations).
@@ -92,6 +92,92 @@ Run the following commands in the Bash environment of Azure Cloud Shell, or afte
9292
az sql server ad-admin create --resource-group myResourceGroup --server-name <server-name> --display-name ADMIN --object-id <entra-id>
9393
```
9494

95+
## Use managed identity connectivity
96+
97+
The following steps configure your app to connect to Azure SQL Database with a system-assigned managed identity. To use a user-assigned identity, see [Tutorial: Connect to Azure databases from App Service without secrets using a managed identity](tutorial-connect-msi-azure-database.md).
98+
99+
### Enable managed identity for the app
100+
101+
To enable a managed identity for your Azure app, use the [az webapp identity assign](/cli/azure/webapp/identity#az-webapp-identity-assign) command, replacing `<app-name>` with your app name.
102+
103+
```azurecli
104+
az webapp identity assign --resource-group myResourceGroup --name <app-name>
105+
```
106+
107+
Here's an example of the output:
108+
109+
```output
110+
{
111+
"additionalProperties": {},
112+
"principalId": "aaaaaaaa-bbbb-cccc-1111-222222222222",
113+
"tenantId": "aaaabbbb-0000-cccc-1111-dddd2222eeee",
114+
"type": "SystemAssigned"
115+
}
116+
```
117+
118+
To enable managed identity for a [deployment slot](deploy-staging-slots.md), add `--slot <slot-name>` and use the name of the slot in `<slot-name>`.
119+
120+
You can also add the identity to a [Microsoft Entra group](/azure/active-directory/fundamentals/active-directory-manage-groups), then grant SQL Database access to the Microsoft Entra group instead of to the identity. The following commands add the example managed identity to a new group called `myAzureSQLDBAccessGroup`.
121+
122+
```azurecli
123+
$groupid=(az ad group create --display-name myAzureSQLDBAccessGroup --mail-nickname myAzureSQLDBAccessGroup --query objectId --output tsv)
124+
$msiobjectid=(az webapp identity show --resource-group myResourceGroup --name <app-name> --query principalId --output tsv)
125+
az ad group member add --group $groupid --member-id $msiobjectid
126+
az ad group member list -g $groupid
127+
```
128+
129+
### Grant permissions to the managed identity
130+
131+
Go to the Azure portal to grant the minimum permissions your app needs.
132+
133+
If the identity is system-assigned, its name is always the same as the name of your app. The name of a system-assigned identity for a deployment slot is `<app-name>/slots/<slot-name>`. To grant permissions for a Microsoft Entra group, use the group's display name, such as `myAzureSQLDBAccessGroup`.
134+
135+
<!--SQLCMD IS NO LONGER SUPPORTED IN BASH CLOUD SHELL as of April 2025. Use Powershell or portal.
136+
1. In your Bash terminal, sign in to SQL Database by using the following SQLCMD command, replacing `<server-name>` with your server name, `<db-name>` with your database name, and `<aad-user-name>` and `<aad-password>` with your Microsoft Entra user credentials.
137+
138+
```bash
139+
sqlcmd -S <server-name>.database.windows.net -d <db-name> -U <aad-user-name> -P <aad-password> -G -l 30
140+
```
141+
142+
1. In the SQL prompt for the database you want, run the following commands to grant the minimum permissions your app needs, replacing `<identity-name>` with the name of the managed identity in Microsoft Entra ID.
143+
144+
```sql
145+
CREATE USER [<identity-name>] FROM EXTERNAL PROVIDER With OBJECT_ID='xxx';
146+
ALTER ROLE db_datareader ADD MEMBER [<identity-name>];
147+
ALTER ROLE db_datawriter ADD MEMBER [<identity-name>];
148+
ALTER ROLE db_ddladmin ADD MEMBER [<identity-name>];
149+
GO
150+
```
151+
152+
1. Enter `EXIT` to return to the Bash prompt.
153+
154+
Here are portal steps I made up. Not sure if they work since my app doesn't work (database is blank).-->
155+
156+
1. On your web app's page in the Azure portal, select **Identity** from the left navigation menu.
157+
1. On the **System assigned** tab, make sure **Status** is set to **On**.
158+
1. Under **Permissions**, select **Azure role assignments**.
159+
1. On the **Azure role assignments** page, select **Add role assignment (Preview)**.
160+
1. Use the **Add role assignment (Preview)** pane to add each of the following roles:
161+
- **Scope**: Select **SQL**.
162+
- **Subscription**: Select your subscription.
163+
- **Resource**: Select your SQL server.
164+
- **Role**: Select each of the following roles:
165+
- **SQL DB Contributor**
166+
- **SQL Server Contributor**
167+
- **Reader**
168+
After adding each role, select **Save**.
169+
170+
> [!NOTE]
171+
> The backend managed identity services [maintain a token cache](overview-managed-identity.md#configure-target-resource) that updates the token for a target resource only when it expires. If you try to modify your SQL Database permissions after first getting a token with your app, you don't get a new token with updated permissions until the cached token expires.
172+
173+
### Modify the connection string
174+
175+
The same changes you made in *Web.config* or *appsettings.json* work with the managed identity. You can remove the existing connection string that Visual Studio created when it deployed your app the first time. To delete the connection string, run the following command, replacing `<app-name>` with the name of your app.
176+
177+
```azurecli
178+
az webapp config connection-string delete --resource-group myResourceGroup --name <app-name> --setting-names MyDbConnection
179+
```
180+
95181
## Set up your development environment
96182

97183
Set up your chosen development environment and sign in to Azure. For more information about setting up your dev environment for Microsoft Entra authentication, see [Azure Identity client library for .NET](/dotnet/api/overview/azure/Identity-readme).
@@ -201,102 +287,6 @@ You can now start using Microsoft Entra authentication to develop and debug your
201287

202288
---
203289

204-
## Use managed identity connectivity
205-
206-
Next, configure your App Service app to connect to SQL Database with a system-assigned managed identity.
207-
208-
> [!NOTE]
209-
> To use a user-assigned identity, see [Tutorial: Connect to Azure databases from App Service without secrets using a managed identity](tutorial-connect-msi-azure-database.md).
210-
211-
### Enable managed identity for the app
212-
213-
To enable a managed identity for your Azure app, use the [az webapp identity assign](/cli/azure/webapp/identity#az-webapp-identity-assign) command, replacing `<app-name>` with your app name.
214-
215-
```azurecli
216-
az webapp identity assign --resource-group myResourceGroup --name <app-name>
217-
```
218-
219-
Here's an example of the output:
220-
221-
```output
222-
{
223-
"additionalProperties": {},
224-
"principalId": "aaaaaaaa-bbbb-cccc-1111-222222222222",
225-
"tenantId": "aaaabbbb-0000-cccc-1111-dddd2222eeee",
226-
"type": "SystemAssigned"
227-
}
228-
```
229-
230-
> [!NOTE]
231-
> - To enable managed identity for a [deployment slot](deploy-staging-slots.md), add `--slot <slot-name>` and use the name of the slot in `<slot-name>`.
232-
>
233-
> - You can also add the identity to a [Microsoft Entra group](/azure/active-directory/fundamentals/active-directory-manage-groups), then grant SQL Database access to the Microsoft Entra group instead of to the identity. The following commands add the example managed identity from the previous step to a new group called `myAzureSQLDBAccessGroup`.
234-
>
235-
> ```azurecli
236-
> $groupid=(az ad group create --display-name myAzureSQLDBAccessGroup --mail-nickname myAzureSQLDBAccessGroup --query objectId --output tsv)
237-
> $msiobjectid=(az webapp identity show --resource-group myResourceGroup --name <app-name> --query principalId --output tsv)
238-
> az ad group member add --group $groupid --member-id $msiobjectid
239-
> az ad group member list -g $groupid
240-
> ```
241-
242-
### Grant permissions to the managed identity
243-
244-
If the identity is system-assigned, the name is always the same as the name of your App Service app. The name of a system-assigned identity for a [deployment slot](deploy-staging-slots.md) is `<app-name>/slots/<slot-name>`. To grant permissions for a Microsoft Entra group, use the group's display name, such as `myAzureSQLDBAccessGroup`.
245-
246-
<!--SQLCMD IS NO LONGER SUPPORTED IN BASH CLOUD SHELL as of April 2025. Use Powershell or portal.
247-
1. In your Bash terminal, sign in to SQL Database by using the following SQLCMD command, replacing `<server-name>` with your server name, `<db-name>` with your database name, and `<aad-user-name>` and `<aad-password>` with your Microsoft Entra user credentials.
248-
249-
```bash
250-
sqlcmd -S <server-name>.database.windows.net -d <db-name> -U <aad-user-name> -P <aad-password> -G -l 30
251-
```
252-
253-
1. In the SQL prompt for the database you want, run the following commands to grant the minimum permissions your app needs, replacing `<identity-name>` with the name of the managed identity in Microsoft Entra ID.
254-
255-
```sql
256-
CREATE USER [<identity-name>] FROM EXTERNAL PROVIDER With OBJECT_ID='xxx';
257-
ALTER ROLE db_datareader ADD MEMBER [<identity-name>];
258-
ALTER ROLE db_datawriter ADD MEMBER [<identity-name>];
259-
ALTER ROLE db_ddladmin ADD MEMBER [<identity-name>];
260-
GO
261-
```
262-
263-
1. Enter `EXIT` to return to the Bash prompt.
264-
265-
Here are portal steps I made up. Not sure if they work since my app doesn't work (database is blank).-->
266-
267-
#### Set the admin for the server
268-
269-
1. In the Azure portal, go to the page for the Azure SQL server. Select the **Microsoft Entra admin** tile.
270-
1. On the **Microsoft Entra ID** page, select **Set admin**.
271-
1. Select the admin user and then select **Select**.
272-
1. Select **Save**.
273-
274-
#### Grant the minimum permissions your app needs
275-
276-
1. In the portal, go to your web app's page and select **Identity**.
277-
1. On the **System assigned** tab, make sure **Status** is set to **On**.
278-
1. Under **Permissions**, select **Azure role assignments**.
279-
1. On the **Azure role assignments** page, select **Add role assignment (Preview)**.
280-
1. Use the **Add role assignment (Preview)** pane to add each of the following roles:
281-
- **Scope**: Select **SQL**.
282-
- **Subscription**: Select your subscription.
283-
- **Resource**: Select your SQL Server.
284-
- **Role**: Select each of the following roles:
285-
- **SQL DB Contributor**
286-
- **SQL Server Contributor**
287-
- **Reader**
288-
After adding each role, select **Save**.
289-
290-
> [!NOTE]
291-
> The backend managed identity services also [maintain a token cache](overview-managed-identity.md#configure-target-resource) that updates the token for a target resource only when it expires. If you try to modify your SQL Database permissions after trying to get a token with your app, you don't get a new token with updated permissions until the cached token expires.
292-
293-
### Modify the connection string
294-
295-
The same changes you made in *Web.config* or *appsettings.json* work with the managed identity. You can remove the existing connection string that Visual Studio created when it deployed your app the first time. To delete the connection string, run the following command, replacing `<app-name>` with the name of your app.
296-
297-
```azurecli
298-
az webapp config connection-string delete --resource-group myResourceGroup --name <app-name> --setting-names MyDbConnection
299-
```
300290

301291
## Publish your changes
302292

0 commit comments

Comments
 (0)