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/app-service/tutorial-connect-app-access-sql-database-as-user-dotnet.md
+16-15Lines changed: 16 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ This tutorial shows you how to enable [built-in authentication](overview-authent
14
14
- Gives the back-end database (or any other Azure service) more control over who or how much to grant access to its data and functionality.
15
15
- Lets the app tailor its data presentation to the signed-in user.
16
16
17
-
In this tutorial, you'll add Azure Active Directory authentication to the sample web app you deployed in one of the following tutorials:
17
+
In this tutorial, you add Azure Active Directory authentication to the sample web app you deployed in one of the following tutorials:
18
18
19
19
-[Tutorial: Build an ASP.NET app in Azure with Azure SQL Database](app-service-web-tutorial-dotnet-sqldatabase.md)
20
20
-[Tutorial: Build an ASP.NET Core and Azure SQL Database app in Azure App Service](tutorial-dotnetcore-sqldb-app.md)
@@ -85,7 +85,7 @@ First, enable Azure Active Directory authentication to SQL Database by assigning
85
85
az sql server ad-only-auth enable --resource-group <group-name> --server-name <server-name>
86
86
```
87
87
88
-
For more information on adding an Active Directory admin, see [Provision an Azure Active Directory administrator for your server](../azure-sql/database/authentication-aad-configure.md#provision-azure-ad-admin-sql-managed-instance)
88
+
For more information on adding an Active Directory admin, see [Provision Azure AD admin (SQL Database)](/azure/azure-sql/database/authentication-aad-configure#provision-azure-ad-admin-sql-database).
89
89
90
90
## 2. Enable user authentication for your app
91
91
@@ -112,7 +112,7 @@ Currently, your Azure app connects to SQL Database uses SQL authentication (user
112
112
113
113
1. In the **Authentication** page for the app, select your app name under **Identity provider**. This app registration was automatically generated for you. Select **API permissions** in the left menu.
114
114
115
-
1. Select **Add a permission**, then select **Apis my organization uses**.
115
+
1. Select **Add a permission**, then select **APIs my organization uses**.
116
116
117
117
1. Type *Azure SQL Database* in the search box and select the result.
az webapp auth set --resource-group <group-name> --name <app-name> --body "$authSettings"
133
133
```
134
134
135
-
The commands effectively add a `loginParameters` property with additional custom scopes. Here's an explanation of the requested scopes:
135
+
The commands effectively add a `loginParameters` property with extra custom scopes. Here's an explanation of the requested scopes:
136
136
137
137
-`openid`, `profile`, and `email` are requested by App Service by default already. For information, see [OpenID Connect Scopes](../active-directory/develop/v2-permissions-and-consent.md#openid-connect-scopes).
138
138
-`https://database.windows.net/user_impersonation` refers to Azure SQL Database. It's the scope that gives you a JWT token that includes SQL Database as a [token audience](https://wikipedia.org/wiki/JSON_Web_Token).
@@ -186,7 +186,7 @@ public MyDatabaseContext (DbContextOptions<MyDatabaseContext> options, IHttpCont
186
186
187
187
# [ASP.NET](#tab/dotnet)
188
188
189
-
1.**If you came from [Tutorial: Build an ASP.NET app in Azure with SQL Database](app-service-web-tutorial-dotnet-sqldatabase.md)**, you set a connection string in App Service using SQL authentication, with a username and password. Use the following command to remove the connection secrets, but replace *\<group-name>*, *\<app-name>*, *\<db-server-name>*, and *\<db-name>* with yours.
189
+
1.**If you came from [Tutorial: Build an ASP.NET app in Azure with SQL Database](app-service-web-tutorial-dotnet-sqldatabase.md)**, you set a connection string in App Service using SQL authentication, with a username and password. Use the following command to remove the connection secrets, but replace *\<group-name>*, *\<app-name>*, *\<db-server-name>*, and *\<db-name>* with yours.
190
190
191
191
```azurecli-interactive
192
192
az webapp config connection-string set --resource-group <group-name> --name <app-name> --type SQLAzure --settings MyDbConnection="server=tcp:<db-server-name>.database.windows.net;database=<db-name>;"
@@ -206,11 +206,11 @@ public MyDatabaseContext (DbContextOptions<MyDatabaseContext> options, IHttpCont
206
206
az webapp config connection-string set --resource-group <group-name> --name <app-name> --type SQLAzure --settings defaultConnection="server=tcp:<db-server-name>.database.windows.net;database=<db-name>;"
207
207
```
208
208
209
-
1. You would've made your code changes in your GitHub fork, with Visual Studio Code in the browser. From the left menue, select **Source Control**.
209
+
1. You would have made your code changes in your GitHub fork, with Visual Studio Code in the browser. From the left menu, select **Source Control**.
210
210
211
211
1. Type in a commit message like `OBO connect` and select **Commit**.
212
212
213
-
The commit will trigger a deployment to App Service. Wait a few minutes for the deployment to finish.
213
+
The commit triggers a GitHub Actions deployment to App Service. Wait a few minutes for the deployment to finish.
214
214
215
215
-----
216
216
@@ -222,24 +222,25 @@ You should now be able to edit the to-do list as before.
- [Why do I get a `Login failed for user '<token-identified principal>'.` error?]()
228
-
- [How do I add other Azure AD users or groups in Azure SQL Database?]()
229
-
- [How do I debug locally when using App Service authentication?]()
227
+
- [Why do I get a `Login failed for user '<token-identified principal>'.` error?](#why-do-i-get-a-login-failed-for-user-token-identified-principal-error)
228
+
- [How do I add other Azure AD users or groups in Azure SQL Database?](#how-do-i-add-other-azure-ad-users-or-groups-in-azure-sql-database)
229
+
- [How do I debug locally when using App Service authentication?](#how-do-i-debug-locally-when-using-app-service-authentication)
230
+
- [What happens when access tokens expire?](#what-happens-when-access-tokens-expire)
230
231
231
232
#### Why do I get a `Login failed for user '<token-identified principal>'.` error?
232
233
233
234
The most common causes of this error are:
234
235
235
-
- You're running the code locally, and there's no valid token in the `X-MS-TOKEN-AAD-ACCESS-TOKEN` request header. See [How do I debug locally when using App Service authentication?]().
236
+
- You're running the code locally, and there's no valid token in the `X-MS-TOKEN-AAD-ACCESS-TOKEN` request header. See [How do I debug locally when using App Service authentication?](#how-do-i-debug-locally-when-using-app-service-authentication).
236
237
- Azure AD authentication isn't configured on your SQL Database.
237
-
- The signed-in user isn't permitted to connect to the database. See [How do I add other Azure AD users or groups in Azure SQL Database?]().
238
+
- The signed-in user isn't permitted to connect to the database. See [How do I add other Azure AD users or groups in Azure SQL Database?](#how-do-i-add-other-azure-ad-users-or-groups-in-azure-sql-database).
238
239
239
240
#### How do I add other Azure AD users or groups in Azure SQL Database?
240
241
241
-
1. Connect to your database server, such as with [sqlcmd](../azure-sql/database/authentication-aad-configure.md#sqlcmd) or [SSMS](../azure-sql/database/authentication-aad-configure.md#connect-to-the-database-using-ssms-or-ssdt).
242
-
1. [Create contained users mapped to Azure AD identities](../azure-sql/database/authentication-aad-configure.md#create-contained-users-mapped-to-azure-ad-identities) in SQL Database documentation.
242
+
1. Connect to your database server, such as with [sqlcmd](/azure/azure-sql/database/authentication-aad-configure#sqlcmd) or [SSMS](/azure/azure-sql/database/authentication-aad-configure#connect-to-the-database-using-ssms-or-ssdt).
243
+
1. [Create contained users mapped to Azure AD identities](/azure/azure-sql/database/authentication-aad-configure#create-contained-users-mapped-to-azure-ad-identities) in SQL Database documentation.
243
244
244
245
The following Transact-SQL example adds an Azure AD identity to SQL Server and gives it some database roles:
# Tutorial: Connect to SQL Database from .NET App Service without secrets using a managed identity
11
11
12
-
[App Service](overview.md) provides a highly scalable, self-patching web hosting service in Azure. It also provides a [managed identity](overview-managed-identity.md) for your app, which is a turn-key solution for securing access to [Azure SQL Database](/azure/sql-database/) and other Azure services. Managed identities in App Service make your app more secure by eliminating secrets from your app, such as credentials in the connection strings. In this tutorial, you'll add managed identity to the sample web app you built in one of the following tutorials:
12
+
[App Service](overview.md) provides a highly scalable, self-patching web hosting service in Azure. It also provides a [managed identity](overview-managed-identity.md) for your app, which is a turn-key solution for securing access to [Azure SQL Database](/azure/sql-database/) and other Azure services. Managed identities in App Service make your app more secure by eliminating secrets from your app, such as credentials in the connection strings. In this tutorial, you add managed identity to the sample web app you built in one of the following tutorials:
13
13
14
14
-[Tutorial: Build an ASP.NET app in Azure with Azure SQL Database](app-service-web-tutorial-dotnet-sqldatabase.md)
15
15
-[Tutorial: Build an ASP.NET Core and Azure SQL Database app in Azure App Service](tutorial-dotnetcore-sqldb-app.md)
@@ -156,11 +156,11 @@ The steps you follow for your project depends on whether you're using [Entity Fr
156
156
conn.AccessToken = token.Token;
157
157
```
158
158
159
-
This code uses [Azure.Identity.DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) to get a useable token for SQL Database from Azure Active Directory and then adds it to the database connection. While you can customize `DefaultAzureCredential`, by default it's already very versatile. When running in App Service, it uses app's system-assigned managed identity. When running locally, it can get a token using the logged-in identity of Visual Studio, Visual Studio Code, Azure CLI, and Azure PowerShell.
159
+
This code uses [Azure.Identity.DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) to get a useable token for SQL Database from Azure Active Directory and then adds it to the database connection. While you can customize `DefaultAzureCredential`, by default it's already versatile. When it runs in App Service, it uses app's system-assigned managed identity. When it runs locally, it can get a token using the logged-in identity of Visual Studio, Visual Studio Code, Azure CLI, and Azure PowerShell.
160
160
161
161
1. In *Web.config*, find the connection string called `MyDbConnection` and replace its `connectionString` value with `"server=tcp:<server-name>.database.windows.net;database=<db-name>;"`. Replace _\<server-name>_ and _\<db-name>_ with your server name and database name. This connection string is used by the default constructor in *Models/MyDbContext.cs*.
162
162
163
-
That's every thing you need to connect to SQL Database. When debugging in Visual Studio, your code uses the Azure AD user you configured in [2. Set up your dev environment](#2-set-up-your-dev-environment). You'll set up SQL Database later to allow connection from the managed identity of your App Service app.
163
+
That's every thing you need to connect to SQL Database. When you debug in Visual Studio, your code uses the Azure AD user you configured in [2. Set up your dev environment](#2-set-up-your-dev-environment). You'll set up SQL Database later to allow connection from the managed identity of your App Service app.
164
164
165
165
1. Type `Ctrl+F5` to run the app again. The same CRUD app in your browser is now connecting to the Azure SQL Database directly, using Azure AD authentication. This setup lets you run database migrations from Visual Studio.
166
166
@@ -182,7 +182,7 @@ The steps you follow for your project depends on whether you're using [Entity Fr
182
182
> The [Active Directory Default](/sql/connect/ado-net/sql/azure-active-directory-authentication#using-active-directory-default-authentication) authentication type can be used both on your local machine and in Azure App Service. The driver attempts to acquire a token from Azure Active Directory using various means. If the app is deployed, it gets a token from the app's managed identity. If the app is running locally, it tries to get a token from Visual Studio, Visual Studio Code, and Azure CLI.
183
183
>
184
184
185
-
That's everything you need to connect to SQL Database. When debugging in Visual Studio, your code uses the Azure AD user you configured in [2. Set up your dev environment](#2-set-up-your-dev-environment). You'll set up SQL Database later to allow connection from the managed identity of your App Service app. The `DefaultAzureCredential` class caches the token in memory and retrieves it from Azure AD just before expiration. You don't need any custom code to refresh the token.
185
+
That's everything you need to connect to SQL Database. When you debug in Visual Studio, your code uses the Azure AD user you configured in [2. Set up your dev environment](#2-set-up-your-dev-environment). You'll set up SQL Database later to allow connection from the managed identity of your App Service app. The `DefaultAzureCredential` class caches the token in memory and retrieves it from Azure AD just before expiration. You don't need any custom code to refresh the token.
186
186
187
187
1. Type `Ctrl+F5` to run the app again. The same CRUD app in your browser is now connecting to the Azure SQL Database directly, using Azure AD authentication. This setup lets you run database migrations from Visual Studio.
0 commit comments