I’m using Microsoft EntraID
as an authentication provider form my web applicaiton.
By default, the JWT
token that is generated by EntraID has a lifetime between 60
and 90
minutes, which is a bit too short for my requirements.
By reading the Microsoft documentation, it seems you can control the lifetime of access/id tokens by creating a TokenLifetimePolicy
and then assigning it to the app registration that is used to authenticate users.
So this is what I did. First I used the powershell
to create a lifetime policy with a 12 hours lifetime:
$params = @{
definition = @(
'{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"12:00:00"}}'
)
displayName = "12h_token_lifetime"
isOrganizationDefault = $false
}
New-MgPolicyTokenLifetimePolicy -BodyParameter $params
Then I assigned it to my app registration:
New-MgApplicationTokenLifetimePolicyByRef -ApplicationId XXX -OdataId "https://graph.microsoft.com/v1.0/policies/tokenLifetimePolicies/YYY"
everything seems to work well, and If I run the Get-MgApplicationTokenLifetimePolicy
it reports that the policy is assigned:
However, even if the policy seems to be applied nothing has changed. When I authenticate to the service (either via Postman or my actual web app, makes no difference), I get a token with the usual lifetime in the 60-90 minutes range:
What am I missing here?
2
Answers
What I remember the token lifetime policy set by
New-MgApplicationTokenLifetimePolicyByRef
is not based on the Entra application used to request the resource, but on the API resource your application is trying to access.For example, when an application registered in Entra has API permissions defined for the Graph API, then the token lifetime policy must be assigned to the service principal related to the Graph API.
I agree with @user2250152, token lifetime policy will be applied only on resource service principals. Initially, I ran same script as you in my environment and got below results:
Response:
Now, I assigned this policy to one application by running below command:
Response:
When I generated the access token with Microsoft Graph scope with this app Id, access token lifetime did not change as below:
But if you generate the access token with resource API scope of assigned application, it will give the access token having
12 hrs
lifetime successfully as below:There is an option to set the parameter
IsOrganizationDefault = $true
while running the script but it makes all service principals in your tenant to generate access token valid for 12 hrs no matter what scope you specify.As mentioned here, you need to have
Microsoft Entra ID P1
license to use that feature. If you are having M365 Business Standard, it’s not enough and you need to update it to Microsoft 365 Business Premium.Reference:
Assigning token lifetime policy to app registration Microsoft Graph. – Microsoft Q&A by Fabio Andrade