Skip to content

Commit 651555e

Browse files
committed
draft
1 parent f87d0ae commit 651555e

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

articles/app-service/configure-authentication-oauth-tokens.md

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,50 @@
11
---
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.
44
ms.topic: how-to
5-
ms.date: 03/29/2021
5+
ms.date: 06/26/2025
66
ms.custom: AppServiceIdentity
77
author: cephalin
88
ms.author: cephalin
99
---
1010

11-
# Work with OAuth tokens in Azure App Service authentication
11+
# Manage OAuth tokens in Azure App Service
1212

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.
1414

1515
## Retrieve tokens in app code
1616

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.
1818

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:
2023

2124
| Provider | Header names |
2225
|-|-|
2326
| 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` |
24-
| Facebook Token | `X-MS-TOKEN-FACEBOOK-ACCESS-TOKEN` <br/> `X-MS-TOKEN-FACEBOOK-EXPIRES-ON` |
27+
| Facebook | `X-MS-TOKEN-FACEBOOK-ACCESS-TOKEN` <br/> `X-MS-TOKEN-FACEBOOK-EXPIRES-ON` |
2528
| 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` |
2629
| X | `X-MS-TOKEN-TWITTER-ACCESS-TOKEN` <br/> `X-MS-TOKEN-TWITTER-ACCESS-TOKEN-SECRET` |
2730

2831
> [!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.
3533
3634
## Refresh auth tokens
3735

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.
4837

49-
```json
50-
"identityProviders": {
51-
"azureActiveDirectory": {
52-
"login": {
53-
"loginParameters": ["scope=openid profile email offline_access"]
54-
}
55-
}
56-
}
57-
```
38+
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.
5839

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:
6041

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)). |
6348

6449
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.
6550

@@ -76,23 +61,45 @@ function refreshTokens() {
7661
}
7762
```
7863

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:
74+
75+
```json
76+
"identityProviders": {
77+
"azureActiveDirectory": {
78+
"login": {
79+
"loginParameters": ["scope=openid profile email offline_access"]
80+
}
81+
}
82+
}
83+
```
84+
85+
1. Select **Put**.
86+
87+
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).
8088

8189
## Extend session token expiration grace period
8290

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.
8492

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.
8694

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):
8896

8997
```azurecli-interactive
9098
az webapp auth update --resource-group <group_name> --name <app_name> --token-refresh-extension-hours <hours>
9199
```
92100

93101
> [!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.
96103
97104
## Related content
98105

0 commit comments

Comments
 (0)