0
votes

I get the following error randomly when I work in my app:

Invalid JWT token. The token is expired.

[SecurityTokenExpiredException: Invalid JWT token. The token is expired.]
Microsoft.IdentityModel.S2S.Tokens.JsonWebSecurityTokenHandler.ValidateLifetime(JsonWebSecurityToken token) +296
Microsoft.IdentityModel.S2S.Tokens.JsonWebSecurityTokenHandler.ValidateTokenCore(SecurityToken token, Boolean isActorToken) +162
EventsWeb.TokenHelper.ReadAndValidateContextToken(String contextTokenString, String appHostName) in C:\APP\TokenHelper.cs:120
EventsWeb.TokenHelper.GetClientContextWithContextToken(String targetUrl, String contextTokenString, String appHostUrl) in C:\APP\TokenHelper.cs:465
EventsWeb.Pages.User.tmpl.index.GetHostWebClientContext() in C:\APP\Pages\User\tmpl\index.aspx.cs:90
EventsWeb.Pages.User.tmpl.index.OnPreRender(EventArgs e) in C:\APP\Pages\User\tmpl\index.aspx.cs:69
System.Web.UI.Control.PreRenderRecursiveInternal() +107
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +7675

There is no way except of restarting the Internet Explorer to solve this issue. I have this in some applications. The ClientId and ClientSecret is valid, otherwise the restart of IE won't be able to solve this problem.

The time when this message occurs differs. Sometimes after 1hour sometimes after 5...

I don't know why the token is getting invalid due the working in this app. But thats not userfriendly...

Any idea about this?

Thanks

1
The add-in uses auth code to request an access token from ACS that sends the access and refresh tokens to the user. By default access tokens are good up to 12 hours and refresh tokens are good for 6 months. For more details check following urls: [1]: msdn.microsoft.com/en-us/library/jj687470.aspx [2]: msdn.microsoft.com/en-us/library/dn762763.aspxnizzik
Ok thanks. I took a look at the link. But is won't give me an good idea to solve this. The token should be valid up to 12hours, but I have still no idea why this error occurs....AJRames
Is this problem already solve?I encountered this error too but I have no idea how to solve it.Datz Me

1 Answers

0
votes

The below two scenarios can cause token expire:

  1. Users have long running sessions with your add-in in which the add-in makes calls to SharePoint many hours (currently more than 12) after it is launched.
  2. The add-in's design enables users to schedule the add-in to access SharePoint sometime after the session ends.

You could use the refresh token to obtain another access token from ACS in your code .The refresh token lasts a few months and can be persisted in a cookie or in server-side storage. Try to use SharePointContext helper class , it could renew the access token if it is not valid automatically .