|
| 1 | +--- |
| 2 | +title: Set lifetimes for tokens |
| 3 | +description: Learn how to set lifetimes for access tokens issued by Microsoft identity platform. |
| 4 | +services: active-directory |
| 5 | +author: rwike77 |
| 6 | +manager: CelesteDG |
| 7 | + |
| 8 | +ms.service: active-directory |
| 9 | +ms.subservice: develop |
| 10 | +ms.workload: identity |
| 11 | +ms.topic: how-to |
| 12 | +ms.date: 04/04/2023 |
| 13 | +ms.author: ryanwi |
| 14 | +ms.custom: identityplatformtop40, contperf-fy21q2, engagement-fy23 |
| 15 | +ms.reviewer: ludwignick |
| 16 | +--- |
| 17 | +# Configure token lifetime policies (preview) |
| 18 | + |
| 19 | +In the following steps, you'll implement a common policy scenario that imposes new rules for token lifetime. It's possible to specify the lifetime of an access, SAML, or ID token issued by the Microsoft identity platform. This can be set for all apps in your organization or for a specific service principal. They can also be set for multi-organizations (multi-tenant application). |
| 20 | + |
| 21 | +For more information, see [configurable token lifetimes](active-directory-configurable-token-lifetimes.md). |
| 22 | + |
| 23 | +## Get started |
| 24 | + |
| 25 | +To get started, download the latest [Microsoft Graph PowerShell SDK](/powershell/microsoftgraph/installation). |
| 26 | + |
| 27 | +## Create a policy for web sign-in |
| 28 | + |
| 29 | +In the following steps, you'll create a policy that requires users to authenticate less frequently in your web app. This policy sets the lifetime of the access/ID tokens for your web app. |
| 30 | + |
| 31 | +```powershell |
| 32 | +Connect-MgGraph -Scopes "Policy.ReadWrite.ApplicationConfiguration" |
| 33 | +
|
| 34 | +# Create a token lifetime policy |
| 35 | +$params = @{ |
| 36 | + Definition = @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"4:00:00"}}') |
| 37 | + DisplayName = "WebPolicyScenario" |
| 38 | + IsOrganizationDefault = $false |
| 39 | +} |
| 40 | +$tokenLifetimePolicyId=(New-MgPolicyTokenLifetimePolicy -BodyParameter $params).Id |
| 41 | +
|
| 42 | +# Display the policy |
| 43 | +Get-MgPolicyTokenLifetimePolicy -TokenLifetimePolicyId $tokenLifetimePolicyId |
| 44 | +
|
| 45 | +# Assign the token lifetime policy to an app |
| 46 | +$params = @{ |
| 47 | + "@odata.id" = "https://graph.microsoft.com/v1.0/policies/tokenLifetimePolicies/$tokenLifetimePolicyId" |
| 48 | +} |
| 49 | +
|
| 50 | +$applicationObjectId="11111111-1111-1111-1111-111111111111" |
| 51 | +
|
| 52 | +New-MgApplicationTokenLifetimePolicyByRef -ApplicationId $applicationObjectId -BodyParameter $params |
| 53 | +
|
| 54 | +# List the token lifetime policy on the app |
| 55 | +Get-MgApplicationTokenLifetimePolicy -ApplicationId $applicationObjectId |
| 56 | +
|
| 57 | +# Remove the policy from the app |
| 58 | +Remove-MgApplicationTokenLifetimePolicyByRef -ApplicationId $applicationObjectId -TokenLifetimePolicyId $tokenLifetimePolicyId |
| 59 | +
|
| 60 | +# Delete the policy |
| 61 | +Remove-MgPolicyTokenLifetimePolicy -TokenLifetimePolicyId $tokenLifetimePolicyId |
| 62 | +``` |
| 63 | + |
| 64 | +## View existing policies in a tenant |
| 65 | + |
| 66 | +To see all policies that have been created in your organization, run the [Get-MgPolicyTokenLifetimePolicy](/powershell/module/microsoft.graph.identity.signins/get-mgpolicytokenlifetimepolicy) cmdlet. Any results with defined property values that differ from the defaults listed above are in scope of the retirement. |
| 67 | + |
| 68 | +```powershell |
| 69 | +Get-MgPolicyTokenLifetimePolicy |
| 70 | +``` |
| 71 | + |
| 72 | +To see which apps are linked to a specific policy that you identified, run [List appliesTo](/graph/api/tokenlifetimepolicy-list-appliesto) with any of your policy IDs. |
| 73 | + |
| 74 | +```powershell |
| 75 | +GET https://graph.microsoft.com/v1.0/policies/tokenLifetimePolicies/4d2f137b-e8a9-46da-a5c3-cc85b2b840a4/appliesTo |
| 76 | +``` |
| 77 | + |
| 78 | +## Next steps |
| 79 | +Learn about [authentication session management capabilities](../conditional-access/howto-conditional-access-session-lifetime.md) in Azure AD Conditional Access. |
0 commit comments