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/configure-authentication-oauth-tokens.md
+49-42Lines changed: 49 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,65 +1,50 @@
1
1
---
2
-
title: Work with OAuth Tokens in AuthN/AuthZ
3
-
description: Learn how to retrieve tokens, refresh tokens, and extend sessions when you use the built-in authentication and authorization in Azure App Service.
2
+
title: Work with OAuth Tokens in Authentication and Authorization
3
+
description: Learn how to retrieve tokens, refresh tokens, and extend session token expiration when you use the built-in authentication and authorization in Azure App Service.
4
4
ms.topic: how-to
5
-
ms.date: 03/29/2021
5
+
ms.date: 06/26/2025
6
6
ms.custom: AppServiceIdentity
7
7
author: cephalin
8
8
ms.author: cephalin
9
9
---
10
10
11
-
# Work with OAuth tokens in Azure App Service authentication
11
+
# Manage OAuth tokens in Azure App Service
12
12
13
-
This article shows you how to work with OAuth tokens when you use the built-in [authentication and authorization in Azure App Service](overview-authentication-authorization.md).
13
+
This article shows you how to manage OAuth tokens when you use [built-in authentication and authorization](overview-authentication-authorization.md) in Azure App Service.
14
14
15
15
## Retrieve tokens in app code
16
16
17
-
From your server code, the provider-specific tokens are injected into the request header so that you can easily access them.
17
+
Provider-specific tokens are injected into the request header from your server code so you can easily access them. To get the provider-specific tokens, send an HTTP `GET` request to `/.auth/me`from your client code, such as a mobile app or in-browser JavaScript. [Token store](overview-authentication-authorization.md#token-store) must be enabled for the app. The returned JSON has the provider-specific tokens.
18
18
19
-
The following table lists possible token header names:
19
+
> [!NOTE]
20
+
> Access tokens are for accessing provider resources, so are present only if you configure your provider with a client secret.
21
+
22
+
The following table lists possible token headers from several providers:
20
23
21
24
| Provider | Header names |
22
25
|-|-|
23
26
| Microsoft Entra |`X-MS-TOKEN-AAD-ID-TOKEN` <br/> `X-MS-TOKEN-AAD-ACCESS-TOKEN` <br/> `X-MS-TOKEN-AAD-EXPIRES-ON` <br/> `X-MS-TOKEN-AAD-REFRESH-TOKEN`|
| Google |`X-MS-TOKEN-GOOGLE-ID-TOKEN` <br/> `X-MS-TOKEN-GOOGLE-ACCESS-TOKEN` <br/> `X-MS-TOKEN-GOOGLE-EXPIRES-ON` <br/> `X-MS-TOKEN-GOOGLE-REFRESH-TOKEN`|
26
29
| X |`X-MS-TOKEN-TWITTER-ACCESS-TOKEN` <br/> `X-MS-TOKEN-TWITTER-ACCESS-TOKEN-SECRET`|
27
30
28
31
> [!NOTE]
29
-
> Different language frameworks might present these headers to the app code in different formats, such as in lowercase or by using title case.
30
-
31
-
From your client code (such as a mobile app or in-browser JavaScript), send an HTTP `GET` request to `/.auth/me` ([token store](overview-authentication-authorization.md#token-store) must be enabled). The returned JSON has the provider-specific tokens.
32
-
33
-
> [!NOTE]
34
-
> Access tokens are for accessing provider resources, so they're present only if you configure your provider with a client secret.
32
+
> Different language frameworks might present these headers to the app code in different formats, such as lowercase or title case.
35
33
36
34
## Refresh auth tokens
37
35
38
-
When your provider's access token (not the [session token](#extend-session-token-expiration-grace-period)) expires, you need to reauthenticate the user before you use that token again. You can avoid token expiration by making a `GET` call to the `/.auth/refresh` endpoint of your application. When called, App Service automatically refreshes the access tokens in the [token store](overview-authentication-authorization.md#token-store) for the authenticated user. Subsequent requests for tokens by your app code get the refreshed tokens. However, for token refresh to work, the token store must contain [refresh tokens](/entra/identity-platform/refresh-tokens) for your provider. The way to get refresh tokens is documented by each provider, but the following list is a brief summary:
39
-
40
-
-**Google**: Append an `access_type=offline` query string parameter to your `/.auth/login/google` API call. For more information, see [Google Refresh Tokens](https://developers.google.com/identity/protocols/OpenIDConnect#refresh-tokens).
41
-
-**Facebook**: Doesn't provide refresh tokens. Long-lived tokens expire in 60 days (see [Long-Lived Access Tokens](https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-long-lived/)).
42
-
-**X**: Access tokens don't expire (see [OAuth FAQ](https://developer.x.com/en/docs/authentication/faq)).
43
-
-**Microsoft**: In [https://resources.azure.com](https://resources.azure.com), do the following steps:
44
-
1. At the top of the pane, select **Read/Write**.
45
-
1. On the explorer menu, go to **subscriptions** > *subscription name* > **resourceGroups** > *resource group name* > **providers** > **Microsoft.Web** > **sites** > *app name* > **config** > **authsettingsV2**.
46
-
1. Select **Edit**.
47
-
1. Modify the following property:
36
+
The following information refers to refreshing provider tokens, not [session tokens](#extend-session-token-expiration-grace-period). If your provider's access token expires, you need to reauthenticate the user before you can use that token again.
You can avoid token expiration by making a `GET` call to the `/.auth/refresh` endpoint of your application. When called, App Service automatically refreshes the access tokens in the [token store](overview-authentication-authorization.md#token-store) for the authenticated user. Subsequent requests for tokens get the refreshed tokens.
58
39
59
-
1. Select **Put**.
40
+
For token refresh to work, the token store must contain [refresh tokens](/entra/identity-platform/refresh-tokens) for your provider. Each provider documents the way to get their refresh tokens. The following table provides a brief summary:
60
41
61
-
> [!NOTE]
62
-
> The scope that gives you a refresh token is [offline_access](../active-directory/develop/v2-permissions-and-consent.md#offline_access). See how it's used in [Tutorial: Authenticate and authorize users end to end in Azure App Service](tutorial-auth-aad.md). The other scopes are already requested by App Service by default. For information on these default scopes, see [OpenID Connect Scopes](../active-directory/develop/v2-permissions-and-consent.md#openid-connect-scopes).
42
+
| Provider | Refresh token |
43
+
|-|-|
44
+
| Microsoft | Follow the procedure in [Get Microsoft refresh tokens](#get-microsoft-refresh-tokens). |
45
+
| Facebook | Doesn't provide refresh tokens. Long-lived tokens expire in 60 days. For more information, see [Long-Lived Access Tokens](https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-long-lived/). |
46
+
| Google | Append an `access_type=offline` query string parameter to your `/.auth/login/google` API call. For more information, see [Google Refresh Tokens](https://developers.google.com/identity/protocols/OpenIDConnect#refresh-tokens).|
47
+
| X | Access tokens don't expire. For more information, see [OAuth FAQ](https://developer.x.com/en/docs/authentication/faq)). |
63
48
64
49
After your provider is configured, you can [find the refresh token and the expiration time for the access token](#retrieve-tokens-in-app-code) in the token store.
65
50
@@ -76,23 +61,45 @@ function refreshTokens() {
76
61
}
77
62
```
78
63
79
-
If a user revokes the permissions granted to your app, your call to `/.auth/me` might fail with a `403 Forbidden` response. To diagnose errors, check your application logs for details.
64
+
If a user revokes the permissions they granted to your app, your call to `/.auth/me` might fail with a `403 Forbidden` response. To diagnose errors, check your application logs for details.
65
+
66
+
### Get Microsoft refresh tokens
67
+
68
+
To get the refresh tokens, take the following steps in [Azure Resource Explorer](https://resources.azure.com).
69
+
70
+
1. At the top of the pane, select **Read/Write**.
71
+
1. In the left pane, expand **subscriptions** > **\<subscription name>** > **resourceGroups** > **\<resource-group-name>** > **providers** > **Microsoft.Web** > **sites** > **\<app-name>** > **config** > **authsettingsV2**.
72
+
1. Select **Edit**.
73
+
1. Modify the following `loginParameters` property:
The scope that gives you a refresh token is [offline_access](/entra/identity-platform/scopes-oidc#the-offline_access-scope). The other scopes are already requested by App Service by default. For information on these default scopes, see [OpenID Connect Scopes](/entra/identity-platform/scopes-oidc#openid-connect-scopes).
80
88
81
89
## Extend session token expiration grace period
82
90
83
-
The authenticated session expires after 8 hours. After an authenticated session expires, a 72-hour grace period follows by default. Within this grace period, you're allowed to refresh the session token with App Service without reauthenticating the user. You can just call`/.auth/refresh` when your session token becomes invalid, and you don't need to track token expiration yourself. When the 72-hour grace period lapses, the user must sign in again to get a valid session token.
91
+
The authenticated session expires after 8 hours, and a 72-hour default grace period follows. Within this grace period, you can refresh the session token with App Service without reauthenticating the user. Call`/.auth/refresh` when your session token becomes invalid, and you don't need to track token expiration yourself. When the 72-hour grace period lapses, the user must sign in again to get a valid session token.
84
92
85
-
If 72 hours isn't enough time for you, you can extend this expiration window. Extending the expiration over a long period could have significant security implications (such as when an authentication token is leaked or stolen). We recommend that you leave the setting at the default 72 hours or set the extension period to the smallest value.
93
+
If you need a longer expiration window than 72 hours, you can extend it. Extending the expiration for a long period could have significant security implications if an authentication token is leaked or stolen. It's best to leave the setting at the default 72 hours or set the extension period to the smallest possible value.
86
94
87
-
To extend the default expiration window, run the following command in [Azure Cloud Shell](../cloud-shell/overview.md).
95
+
To extend the default expiration window, run the following Azure CLI command in [Azure Cloud Shell](../cloud-shell/overview.md):
88
96
89
97
```azurecli-interactive
90
98
az webapp auth update --resource-group <group_name> --name <app_name> --token-refresh-extension-hours <hours>
91
99
```
92
100
93
101
> [!NOTE]
94
-
> The grace period only applies to the App Service authenticated session, not to the tokens from the identity providers. No grace period exists for expired provider tokens.
95
-
>
102
+
> The grace period applies only to the App Service authenticated session, not to the access tokens from the identity providers. No grace period exists for expired provider tokens.
0 commit comments