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-aad-sql-database.md
+36-98Lines changed: 36 additions & 98 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,11 @@ description: Secure database connectivity with Azure Active Directory authentica
4
4
5
5
ms.devlang: csharp
6
6
ms.topic: tutorial
7
-
ms.date: 03/04/2022
7
+
ms.date: 03/07/2022
8
8
---
9
-
# Tutorial: Connect to SQL Database from .NET App Service without secrets using a managed identity
9
+
# Tutorial: Connect an App Service app to SQL Database on behalf of the signed-in user
10
10
11
-
[App Service](overview.md) provides a highly scalable, self-patching web hosting service in Azure. It also provides [user authentication](overview-authentication-authorization.md) for your app, which is a turn-key solution for securing access to your app. Paired with the Azure Active Directory authentication provider, you're able to connect to any Azure service that supports Azure Active Directory authentication [onbehalf of](../active-directory/develop/v2-oauth2-on-behalf-of-flow.md) the signed-in user (also known as user impersonation). This is a more advanced connectivity approach to [Tutorial: Access data with managed identity](tutorial-connect-msi-sql-database.md) and has the following advantages in enterprise scenarios:
11
+
[App Service](overview.md) provides a highly scalable, self-patching web hosting service in Azure. It also provides [user authentication](overview-authentication-authorization.md) for your app, which is a turn-key solution for securing access to your app. Paired with the Azure Active Directory authentication provider, you're able to connect to any Azure service by impersonating the signed-in user (also known as the [on-behalf-of flow](../active-directory/develop/v2-oauth2-on-behalf-of-flow.md)) as long as the service supports Azure Active Directory authentication. This is a more advanced connectivity approach to [Tutorial: Access data with managed identity](tutorial-connect-msi-sql-database.md) and has the following advantages in enterprise scenarios:
12
12
13
13
- Eliminates connection secrets to back-end services, just like the managed identity approach.
14
14
- Gives the back-end service more control over who or how much to grant access to its data and functionality.
@@ -33,10 +33,10 @@ When you're finished, your sample app will connect to SQL Database securely on b
33
33
What you will learn:
34
34
35
35
> [!div class="checklist"]
36
-
> * Enable managed identities
37
-
> *Grant SQL Database access to the managed identity
38
-
> *Configure Entity Framework to use Azure AD authentication with SQL Database
39
-
> *Connect to SQL Database from Visual Studio using Azure AD authentication
36
+
> * Enable built-in authentication and authorization
37
+
> *Secure apps against unauthenticated requests
38
+
> *Use Azure Active Directory as the identity provider
39
+
> *Access Azure SQL Database on behalf of the signed-in Azure AD user
40
40
41
41
> [!NOTE]
42
42
>Azure AD authentication is _different_ from [Integrated Windows authentication](/previous-versions/windows/it-pro/windows-server-2003/cc758557(v=ws.10)) in on-premises Active Directory (AD DS). AD DS and Azure AD use completely different authentication protocols. For more information, see [Azure AD Domain Services documentation](../active-directory-domain-services/index.yml).
@@ -52,8 +52,6 @@ This article continues where you left off in either one of the following tutoria
52
52
53
53
If you haven't already, follow one of the two tutorials first. Alternatively, you can adapt the steps for your own .NET app with SQL Database.
54
54
55
-
To debug your app using SQL Database as the back end, make sure that you've allowed client connection from your computer. If not, add the client IP by following the steps at [Manage server-level IP firewall rules using the Azure portal](../azure-sql/database/firewall-configure.md#use-the-azure-portal-to-manage-server-level-ip-firewall-rules).
@@ -82,130 +80,66 @@ First, enable Azure Active Directory authentication to SQL Database by assigning
82
80
83
81
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)
84
82
85
-
## Enable user authentication for your app
83
+
## 2. Enable user authentication for your app
86
84
87
85
You enable authentication with Azure Active Directory as the identity provider. For more information, see [Configure Azure Active Directory authentication for your App Services application](configure-authentication-provider-aad.md).
88
86
89
87
1. In the [Azure portal](https://portal.azure.com) menu, select **Resource groups** or search for and select *Resource groups* from any page.
90
88
91
-
1. In **Resource groups**, find and select your resource group. In **Overview**, select your back-end app's management page.
89
+
1. In **Resource groups**, find and select your resource group. In **Overview**, select your app's management page.
92
90
93
-
:::image type="content" source="./media/tutorial-auth-aad/portal-navigate-back-end.png" alt-text="Screenshot of the Resource groups window, showing the Overview for an example resource group and a back-end app's management page selected.":::
91
+
:::image type="content" source="./media/tutorial-auth-aad/portal-navigate-back-end.png" alt-text="Screenshot of the Resource groups window, showing the Overview for an example resource group and an app's management page selected.":::
94
92
95
-
1. In your back-end app's left menu, select **Authentication**, and then click **Add identity provider**.
93
+
1. In your app's left menu, select **Authentication**, and then select **Add identity provider**.
96
94
97
95
1. In the **Add an identity provider** page, select **Microsoft** as the **Identity provider** to sign in Microsoft and Azure AD identities.
98
96
99
-
1. Accept the default settings and click **Add**.
97
+
1. Accept the default settings and select **Add**.
100
98
101
-
:::image type="content" source="./media/tutorial-auth-aad/configure-auth-back-end.png" alt-text="Screenshot of the back-end app's left menu showing Authentication/Authorization selected and settings selected in the right menu.":::
99
+
:::image type="content" source="./media/tutorial-auth-aad/configure-auth-back-end.png" alt-text="Screenshot of the app's left menu showing Authentication/Authorization selected and settings selected in the right menu.":::
102
100
103
101
> [!TIP]
104
102
> If you run into errors and reconfigure your app's authentication/authorization settings, the tokens in the token store may not be regenerated from the new settings. To make sure your tokens are regenerated, you need to sign out and sign back in to your app. An easy way to do it is to use your browser in private mode, and close and reopen the browser in private mode after changing the settings in your apps.
105
103
106
-
## Configure user impersonation to SQL Database
104
+
## 3. Configure user impersonation to SQL Database
107
105
108
-
Currently, your Azure app connects to SQL Database uses SQL authencation (username and password) managed as app settings. In this step, you give the app permissions to access SQL Database on behalf of the signed-in Azure AD user.
106
+
Currently, your Azure app connects to SQL Database uses SQL authentication (username and password) managed as app settings. In this step, you give the app permissions to access SQL Database on behalf of the signed-in Azure AD user.
109
107
110
-
1. In the **Authentication** page for the front-end app, select your front-end app name under **Identity provider**. This app registration was automatically generated for you. Select **API permissions** in the left menu.
108
+
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.
111
109
112
110
1. Select **Add a permission**, then select **Apis my organization uses**.
113
111
114
112
1. Type *Azure SQL Database* in the search box and select the result.
115
113
116
-
1. In the **Request API permissions** page for the back-end app, select **Delegated permissions** and **user_impersonation**, then select **Add permissions**.
114
+
1. In the **Request API permissions** page for Azure SQL Database, select **Delegated permissions** and **user_impersonation**, then select **Add permissions**.
117
115
118
116
:::image type="content" source="./media/tutorial-auth-aad/select-permission-front-end.png" alt-text="Screenshot of the Request API permissions page showing Delegated permissions, user_impersonation, and the Add permission button selected.":::
119
117
120
-
### Configure App Service to return a usable access token
118
+
## 4. Configure App Service to return a usable access token
121
119
122
120
The app registration in Azure Active Directory now has the required permissions to connect to SQL Database by impersonating the signed-in user. Next, you configure your App Service app to give you a usable access token.
123
121
124
-
In the Cloud Shell, run the following commands on the front-end app to add the `scope` parameter to the authentication setting `identityProviders.azureActiveDirectory.login.loginParameters`.
122
+
In the Cloud Shell, run the following commands on the app to add the `scope` parameter to the authentication setting `identityProviders.azureActiveDirectory.login.loginParameters`.
125
123
126
124
```azurecli-interactive
127
125
authSettings=$(az webapp auth show --resource-group <group-name> --name <app-name>)
az webapp auth set --resource-group <group-name> --name <app-name> --body "$authSettings"
130
128
```
131
129
132
-
The commands effectively adds a `loginParameters` property with additional custom scopes. Here's an explanation of the requested scopes:
130
+
The commands effectively add a `loginParameters` property with additional custom scopes. Here's an explanation of the requested scopes:
133
131
134
132
-`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).
135
133
-`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).
136
-
-[offline_access](../active-directory/develop/v2-permissions-and-consent.md#offline_access) is included here for convenience (in case you want to [refresh tokens](#when-access-tokens-expire)).
134
+
-[offline_access](../active-directory/develop/v2-permissions-and-consent.md#offline_access) is included here for convenience (in case you want to [refresh tokens](#what-happens-when-access-tokens-expire)).
137
135
138
136
> [!TIP]
139
137
> To configure the required scopes using a web interface instead, see the Microsoft steps at [Refresh auth tokens](configure-authentication-oauth-tokens.md#refresh-auth-tokens).
140
138
141
139
142
-
::: zone pivot="platform-linux"
143
-
144
-
> [!NOTE]
145
-
> For Linux apps, There's a temporary requirement to configure a versioning setting for the back-end app registration. In the Cloud Shell, configure it with the following commands. Be sure to replace *\<back-end-client-id>* with your back end's client ID.
146
-
>
147
-
> ```azurecli-interactive
148
-
> id=$(az ad app show --id <back-end-client-id> --query objectId --output tsv)
149
-
> az rest --method PATCH --url https://graph.microsoft.com/v1.0/applications/$id --body "{'api':{'requestedAccessTokenVersion':2}}"
150
-
> ```
151
-
152
-
::: zone-end
153
-
154
-
Your apps are now configured. The front end is now ready to access the back end with a proper access token.
155
-
156
-
## 2. Set up your dev environment
157
-
158
-
# [Visual Studio Windows](#tab/windowsclient)
159
-
160
-
1. Visual Studio for Windows is integrated with Azure AD authentication. To enable development and debugging in Visual Studio, add your Azure AD user in Visual Studio by selecting **File** > **Account Settings** from the menu, and select **Sign in** or **Add**.
161
-
162
-
1. To set the Azure AD user for Azure service authentication, select **Tools** > **Options** from the menu, then select **Azure Service Authentication** > **Account Selection**. Select the Azure AD user you added and select **OK**.
163
-
164
-
# [Visual Studio for macOS](#tab/macosclient)
165
-
166
-
1. Visual Studio for Mac is *not* integrated with Azure AD authentication. However, the Azure Identity client library that you'll use later can use tokens from Azure CLI. To enable development and debugging in Visual Studio, [install Azure CLI](/cli/azure/install-azure-cli) on your local machine.
167
-
168
-
1. Sign in to Azure CLI with the following command using your Azure AD user:
169
-
170
-
```azurecli
171
-
az login --allow-no-subscriptions
172
-
```
173
-
174
-
# [Visual Studio Code](#tab/vscode)
175
-
176
-
1. Visual Studio Code is integrated with Azure AD authentication through the Azure extension. Install the <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-node-azure-pack" target="_blank">Azure Tools</a> extension in Visual Studio Code.
177
-
178
-
1. In Visual Studio Code, in the [Activity Bar](https://code.visualstudio.com/docs/getstarted/userinterface), select the **Azure** logo.
140
+
Your apps are now configured. The app can now generate a token that SQL Database accepts.
179
141
180
-
1. In the **App Service** explorer, select **Sign in to Azure...** and follow the instructions.
181
-
182
-
# [Azure CLI](#tab/cli)
183
-
184
-
1. The Azure Identity client library that you'll use later can use tokens from Azure CLI. To enable command-line based development, [install Azure CLI](/cli/azure/install-azure-cli) on your local machine.
185
-
186
-
1. Sign in to Azure with the following command using your Azure AD user:
187
-
188
-
```azurecli
189
-
az login --allow-no-subscriptions
190
-
```
191
-
192
-
# [Azure PowerShell](#tab/ps)
193
-
194
-
1. The Azure Identity client library that you'll use later can use tokens from Azure PowerShell. To enable command-line based development, [install Azure PowerShell](/powershell/azure/install-az-ps) on your local machine.
195
-
196
-
1. Sign in to Azure CLI with the following cmdlet using your Azure AD user:
197
-
198
-
```powershell-interactive
199
-
Connect-AzAccount
200
-
```
201
-
202
-
-----
203
-
204
-
For more information about setting up your dev environment for Azure Active Directory authentication, see [Azure Identity client library for .NET](/dotnet/api/overview/azure/Identity-readme).
205
-
206
-
You're now ready to develop and debug your app with the SQL Database as the back end, using Azure AD authentication.
207
-
208
-
## 3. Modify your project
142
+
## 5. Modify your project
209
143
210
144
> [!NOTE]
211
145
> **Microsoft.Azure.Services.AppAuthentication** is no longer recommended to use with new Azure SDK.
@@ -249,7 +183,7 @@ public MyDatabaseContext (DbContextOptions<MyDatabaseContext> options, IHttpCont
249
183
>
250
184
> This code change doesn't work locally. For more information, see [How do I debug locally when using App Service authentication?]().
251
185
252
-
## 5. Publish your changes
186
+
## 6. Publish your changes
253
187
254
188
1. In the previous tutorial, 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.
255
189
@@ -297,8 +231,8 @@ You should now be able to edit the to-do list as before.
297
231
The most common causes of this error are:
298
232
299
233
- 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?]().
300
-
- Azure AD authentication is not configured on your SQL Database.
301
-
- The signed-in user is not permitted to connect to the database. See [How do I add other Azure AD users or groups in Azure SQL Database?]().
234
+
- Azure AD authentication isn't configured on your SQL Database.
235
+
- 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?]().
302
236
303
237
#### How do I add other Azure AD users or groups in Azure SQL Database?
304
238
@@ -317,27 +251,31 @@ The most common causes of this error are:
317
251
318
252
#### How do I debug locally when using App Service authentication?
319
253
320
-
Because App Service authentication is a feature in Azure, it's not possible for the same code to work in your local environment. Unlike the app running in Azure, your local code is not connected to an Azure AD app registration. Neither is there any pre-built authentication logic that injects the access token like in App Service. You have a few alternatives:
254
+
Because App Service authentication is a feature in Azure, it's not possible for the same code to work in your local environment. Unlike the app running in Azure, your local code isn't connected to an Azure AD app registration. Neither is there any pre-built authentication logic that injects the access token likein App Service. You have a few alternatives:
321
255
322
256
- Connect to SQL Database from your local environment with [`Active Directory Interactive`](../sql/connect/ado-net/sql/azure-active-directory-authentication.md#using-active-directory-interactive-authentication). The authentication flow doesn't sign in the user to the app itself, but it does connect to the back-end database with the signed-in user, and allows you to test user authorization locally.
323
257
- Manually copy the access token from`https://<app-name>.azurewebsites.net/.auth/me` into your code, in place of the `X-MS-TOKEN-AAD-ACCESS-TOKEN` request header.
324
258
- If you deploy from Visual Studio, use remote debugging of your App Service app.
325
259
260
+
#### What happens when access tokens expire?
261
+
262
+
Your access token expires after some time. For information on how to refresh your access tokens without requiring users to reauthenticate with your app, see [Refresh identity provider tokens](configure-authentication-oauth-tokens.md#refresh-auth-tokens).
263
+
326
264
## Next steps
327
265
328
266
What you learned:
329
267
330
268
> [!div class="checklist"]
331
-
>* Enable managed identities
332
-
>*Grant SQL Database access to the managed identity
333
-
>*Configure Entity Framework to use Azure AD authentication with SQL Database
334
-
>*Connect to SQL Database from Visual Studio using Azure AD authentication
269
+
>* Enable built-in authentication and authorization
270
+
>*Secure apps against unauthenticated requests
271
+
>*Use Azure Active Directory as the identity provider
272
+
>*Access Azure SQL Database on behalf of the signed-inAzure AD user
335
273
336
274
> [!div class="nextstepaction"]
337
275
> [Map an existing custom DNS name to Azure App Service](app-service-web-tutorial-custom-domain.md)
338
276
339
277
> [!div class="nextstepaction"]
340
-
> [Tutorial: Connect to Azure services that don't support managed identities (using Key Vault)](tutorial-connect-msi-key-vault.md)
278
+
> [Tutorial: Access Microsoft Graph from a secured .NET app as the app](scenario-secure-app-access-microsoft-graph-as-app.md)
341
279
342
280
> [!div class="nextstepaction"]
343
281
> [Tutorial: Isolate back-end communication with Virtual Network integration](tutorial-networking-isolate-vnet.md)
0 commit comments