Skip to content

Commit 7419d50

Browse files
committed
edits
1 parent 7e31ae9 commit 7419d50

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ The following steps configure your app to connect to Azure SQL Database with a s
9393

9494
### Enable managed identity for the app
9595

96-
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.
96+
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. The name of a system-assigned identity is always the same as the app name.
9797

9898
```azurecli
9999
az webapp identity assign --resource-group myResourceGroup --name <app-name>
@@ -110,9 +110,9 @@ Here's an example of the output:
110110
}
111111
```
112112

113-
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>`.
113+
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>`. The name of a system-assigned identity for a deployment slot is `<app-name>/slots/<slot-name>`.
114114

115-
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`.
115+
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. To grant permissions for a Microsoft Entra group, use the group's display name. The following commands add the example managed identity to a new group called `myAzureSQLDBAccessGroup`.
116116

117117
```azurecli
118118
$groupid=(az ad group create --display-name myAzureSQLDBAccessGroup --mail-nickname myAzureSQLDBAccessGroup --query objectId --output tsv)
@@ -125,17 +125,15 @@ az ad group member list -g $groupid
125125

126126
Grant the identity the minimum permissions your app needs.
127127

128-
The name of a system-assigned identity is always the same as the app name. 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`.
129-
130-
1. In a PowerShell command line, 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 `<entra-user>` with the Microsoft Entra user name you used to set up the database. This Entra user has admin access to the database server by default.
128+
1. Open a PowerShell command line and 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 `<entra-user>` with the Microsoft Entra user name you used to set up the database. This Entra user has admin access to the database server by default.
131129

132130
```azurepowershell
133131
sqlcmd -S <servername>.database.windows.net -d <db-name> -U <entra-user> -G -l 30
134132
```
135133

136134
Follow the prompts to sign in.
137135

138-
1. At the SQL prompt, 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.
136+
1. At the SQL prompt, run the following commands to grant the minimum permissions your app needs. Replace `<identity-name>` with the name of the managed identity in Microsoft Entra ID, which is the same as the app name.
139137

140138
```sql
141139
CREATE USER [<identity-name>] FROM EXTERNAL PROVIDER;
@@ -148,9 +146,9 @@ The name of a system-assigned identity is always the same as the app name. The n
148146
> [!NOTE]
149147
> 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.
150148
151-
### Remove the existing connection string
149+
### Remove the original connection string
152150

153-
The same changes you made in *Web.config* or *appsettings.json* work with the managed identity. You can remove the existing connection string you used when you deployed your app the first time. To delete the connection string, run the following command, replacing `<app-name>` with the name of your app.
151+
Any changes you made in *web.config* or *appsettings.json* work with the managed identity. You can remove the original connection string you used when you deployed your app the first time. To delete the connection string, run the following Azure CLI command, replacing `<app-name>` with the name of your app.
154152

155153
```azurecli
156154
az webapp config connection-string delete --resource-group myResourceGroup --name <app-name> --setting-names <connection-string-name>
@@ -193,19 +191,16 @@ The Azure Identity client library can use tokens from Azure PowerShell.
193191

194192
## Modify your project and publish your app
195193

196-
You can now use Microsoft Entra authentication to develop and debug your Azure SQL database-backed web app.
197-
198-
The app uses a database context to connect with the database. You update the code to refer to the Entity Framework SQL Server provider, which depends on the modern [Microsoft.Data.SqlClient](https://github.com/dotnet/SqlClient) ADO.NET provider. The Entity Framework provider replaces the built-in `System.Data.SqlClient` SQL Server provider, and includes support for Microsoft Entra ID authentication methods. For more information, see [Microsoft.EntityFramework.SqlServer}](https://www.nuget.org/packages/Microsoft.EntityFramework.SqlServer).
194+
You can now use Microsoft Entra authentication to work with your Azure SQL database-backed web app. The app uses a database context to connect with the database. You update the database context to refer to the Entity Framework SQL Server provider, which depends on the modern [Microsoft.Data.SqlClient](https://github.com/dotnet/SqlClient) ADO.NET provider.
199195

200-
`[DbConfigurationType(typeof(MicrosoftSqlDbConfiguration))]` works locally to use `Microsoft.Data.SqlClient` for the database context, but because `System.Data.SqlClient` is hardcoded as the provider in Azure App Service, you need to extend `MicrosoftSqlDbConfiguration` to redirect references to `System.Data.SqlClient` to `Microsoft.Data.SqlClient` instead.
196+
The Entity Framework provider replaces the built-in `System.Data.SqlClient` SQL Server provider, and includes support for Microsoft Entra ID authentication methods. For more information, see [Microsoft.EntityFramework.SqlServer](https://www.nuget.org/packages/Microsoft.EntityFramework.SqlServer).
201197

202-
The steps differ depending on whether you have an ASP.NET or ASP.NET Core app.
203-
204-
- An ASP.NET Core app uses [Entity Framework Core](/ef/core/) by default.
205-
- An ASP.NET app uses [Entity Framework](/ef/ef6/) by default.
198+
`[DbConfigurationType(typeof(MicrosoftSqlDbConfiguration))]` works locally to use `Microsoft.Data.SqlClient` for the database context, but because `System.Data.SqlClient` is hardcoded as the provider in Azure App Service, you need to extend `MicrosoftSqlDbConfiguration` to redirect references to `System.Data.SqlClient` to `Microsoft.Data.SqlClient` instead. The steps differ depending on whether you have an ASP.NET or ASP.NET Core app.
206199

207200
# [ASP.NET Core app](#tab/efcore)
208201

202+
An ASP.NET Core app uses [Entity Framework Core](/ef/core/) by default.
203+
209204
1. In the Visual Studio **Package Manager Console**, add the NuGet package [Microsoft.Data.SqlClient](https://www.nuget.org/packages/Microsoft.Data.SqlClient).
210205

211206
```powershell
@@ -219,7 +214,7 @@ The steps differ depending on whether you have an ASP.NET or ASP.NET Core app.
219214
```
220215

221216
> [!NOTE]
222-
> You can use [Microsoft Entra Default](/sql/connect/ado-net/sql/azure-active-directory-authentication#using-active-directory-default-authentication) authentication both on your local machine and in Azure App Service. The driver can acquire a token from Microsoft Entra ID in several different ways.
217+
> You can use [Active Directory Default](/sql/connect/ado-net/sql/azure-active-directory-authentication#using-active-directory-default-authentication) authentication both on your local machine and in Azure App Service. The driver can acquire a token from Microsoft Entra ID in several different ways.
223218
>
224219
>If the app is deployed, the driver gets a token from the app's system-assigned managed identity. The driver can also authenticate with a user-assigned managed identity if you include `User Id=<client-id-of-user-assigned-managed-identity>;` in your connection string.
225220
>
@@ -238,6 +233,8 @@ git push azure main
238233

239234
# [ASP.NET app](#tab/ef)
240235

236+
An ASP.NET app uses [Entity Framework](/ef/ef6/) by default.
237+
241238
1. From the Visual Studio **Tools** menu, select **NuGet Package Manager** > **Package Manager Console**.
242239

243240
1. In the **Package Manager Console**, install the following packages:
@@ -267,19 +264,21 @@ git push azure main
267264
[DbConfigurationType(typeof(AppServiceConfiguration))]
268265
```
269266

270-
1. Open *web.config*, find the connection string called `MyDbConnection`, and replace its `connectionString` value with
267+
1. In *web.config*, replace the value of the connection string with the following code, replacing `<server-name` and `<database-name>` with your server name and database name.
271268

272-
`"server=tcp:<server-name>.database.windows.net;Authentication=Active Directory Default; Database=<db-name>;"`
269+
```json
270+
"Server=tcp:<server-name>.database.windows.net;Authentication=Active Directory Default; Database=<database-name>;"
271+
```
273272

274-
replacing `<server-name` and `<db-name>` with your server name and database name. This connection string is used by the default constructor in *Models/MyDatabaseContext.cs*.
273+
This connection string is used by the default constructor in *Models/MyDatabaseContext.cs*.
275274

276275
1. In *web.config*, remove the `entityFramework/providers/provider` section and line: `<provider invariantName="System.Data.SqlClient" .../>`.
277276

278277
You now have everything you need to connect to SQL Database when you debug in Visual Studio. Your code uses the Microsoft Entra user you configured when you set up your dev environment.
279278

280279
1. In Visual Studio, press **Ctrl**+**F5** to run the app. The CRUD app in your browser now connects to the Azure SQL database directly, using Microsoft Entra authentication. This setup lets you run database migrations from Visual Studio.
281280

282-
1. In **Solution Explorer**, right-click your **DotNetAppSqlDb** project and select **Publish**.
281+
1. To publish your app, in **Solution Explorer**, right-click your **DotNetAppSqlDb** project and select **Publish**.
283282

284283
![Screenshot of selecting Publish from Solution Explorer.](media//tutorial-connect-msi-sql-database/solution-explorer-publish.png)
285284

0 commit comments

Comments
 (0)