IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

How to set identity token and access token timeout, my application do not redirect to logout when access token exipres

Open khuramshahzad opened this issue 9 years ago • 28 comments

I have Asp.Net Core application and use AngularJS secured with identity server, I made request to Web API, Web API is secured with Identity server, every thing works fine until some one left website open for some time , may be 20 minutes to so on then issue pop ups, issue is that web application make request to web api, response is some time 401 and some time -1.I use angular http interceptor for that but its not work perfect,

What i need is to setup identity token, and access token timeout like 2 hours after that system should redirect to logout page.

khuramshahzad avatar Mar 01 '17 08:03 khuramshahzad

This seems to be a general question about IdentityServer - not a bug report or an issue.

Please use StackOverflow for that. This has the advantage that questions and answers can be easily found by search engines, and that there are more people answering questions than just us.

For IdentityServer3 https://stackoverflow.com/questions/tagged/?tagnames=identityserver3&sort=newest

For IdentityServer4 https://stackoverflow.com/questions/tagged/?tagnames=identityserver4&sort=newest

For commercial support options - visit https://identityserver.io

leastprivilege avatar Mar 01 '17 09:03 leastprivilege

Agreed, I searched a lot, found some help but it not works in latest version of identity server, now I just want to know how to set Identity Token and Access Token time out in latest version of identity server, if you can help me , I will be very thankful.

khuramshahzad avatar Mar 01 '17 10:03 khuramshahzad

Both are settings on the client object.

leastprivilege avatar Mar 01 '17 11:03 leastprivilege

Thanks I got it, They are setup in identity server project, I was trying to set on client project.

khuramshahzad avatar Mar 01 '17 11:03 khuramshahzad

Dude, do you still here? How did you solve that?

webcrack4 avatar Aug 14 '17 18:08 webcrack4

new Client { ClientId = "WebAppId", ClientName = "Web Application", RequireConsent = false, AllowedGrantTypes = GrantTypes.HybridAndClientCredentials, ClientSecrets = new List { new Secret("secret786".Sha256()) }, //ToDo:: Get urls by environment RedirectUris = GetClientRedirectUris(clientId, environment), //CustomMethod PostLogoutRedirectUris = GetClientPostRedirectUris(clientId, environment), //CustomMethod AllowedCorsOrigins = { "http://localhost:6003" }, AllowedScopes = new List { StandardScopes.OpenId, StandardScopes.Profile, StandardScopes.Email, StandardScopes.OfflineAccess, StandardScopes.Phone }, //This feature refresh token AllowOfflineAccess = true, //Access token life time is 7200 seconds (2 hour) AccessTokenLifetime = 7200, //Identity token life time is 7200 seconds (2 hour) IdentityTokenLifetime = 7200 };

khuramshahzad avatar Aug 14 '17 19:08 khuramshahzad

Thanks

webcrack4 avatar Aug 14 '17 23:08 webcrack4

Welcome

On Tue, Aug 15, 2017 at 4:34 AM, webcrack4 [email protected] wrote:

Thanks

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/IdentityServer/IdentityServer4/issues/857#issuecomment-322338167, or mute the thread https://github.com/notifications/unsubscribe-auth/AFOwsQ9ir0IjYdlApmq46MxulpvZOc09ks5sYNmigaJpZM4MPW68 .

-- Regards Khurram Shahzad

khuramshahzad avatar Aug 17 '17 03:08 khuramshahzad

can i change AccessTokenLifetime at runtime? If yes, how?

gummadikalyan avatar Nov 07 '17 21:11 gummadikalyan

new Client { ClientId = "Abc", ClientName = DEF", RequireConsent = false, AllowedGrantTypes = GrantTypes.HybridAndClientCredentials, ClientSecrets = new List { new Secret("TEST")) }, //ToDo:: Get urls by environment RedirectUris = "your redirect uri", PostLogoutRedirectUris = "", AllowedCorsOrigins = { "https://abc.com" }, AllowedScopes = new List { "webapi_scope", StandardScopes.OpenId, StandardScopes.Profile, StandardScopes.Email, StandardScopes.OfflineAccess, StandardScopes.Phone }, //This feature refresh token AllowOfflineAccess = true, //Access token life time is 7200 seconds (2 hour) AccessTokenLifetime = 7200, //Identity token life time is 7200 seconds (2 hour) IdentityTokenLifetime = 7200 };

On Wed, Nov 8, 2017 at 2:27 AM, gummadikalyan [email protected] wrote:

can i change AccessTokenLifetime at runtime? If yes, how?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/IdentityServer/IdentityServer4/issues/857#issuecomment-342627947, or mute the thread https://github.com/notifications/unsubscribe-auth/AFOwsete8Rz3QPLSJif4tDRJ4NZqlZRZks5s0MtdgaJpZM4MPW68 .

-- Regards Khurram Shahzad

khuramshahzad avatar Nov 08 '17 10:11 khuramshahzad

Solved using Identity Server Entity Framework.

appBuilder.Map("/core", coreApp => { var context = new DatabaseContext(); var efConfig = new EntityFrameworkServiceOptions { ConnectionString = context.ConnectionString, };

            // these two calls just pre-populate the test DB from the in-memory config
            ConfigureClients(Clients.Get(), efConfig);
            ConfigureScopes(Scopes.Get(), efConfig);

            var factory = new IdentityServerServiceFactory();

            factory.CorsPolicyService = new Registration<ICorsPolicyService>(new 
                                                        DefaultCorsPolicyService { AllowAll = true });

            factory.RegisterConfigurationServices(efConfig);
            factory.RegisterOperationalServices(efConfig);

            // note: this registration is a singleton
            factory.UserService = new Registration<IUserService, CustomUserService>();
            var options = new IdentityServerOptions
            {
                SiteName = "Fabric Identity Server",
                SigningCertificate = LoadCertificate(),
                Factory = factory,
                EventsOptions = new EventsOptions
                {
                    RaiseSuccessEvents = true,
                    RaiseFailureEvents = true,
                    RaiseErrorEvents = true,
                    RaiseInformationEvents = true
                },
                RequireSsl = false
            };

            appBuilder.UseIdentityServer(options);
        });

Update the "AccessTokenLifetime " database value using web api Route.

gummadikalyan avatar Nov 08 '17 10:11 gummadikalyan

I set AccessTokenLifeTime to 3600*24*10 = 10 days; but the token still expires after one day. Anyone knows why?

stt106 avatar Nov 20 '17 19:11 stt106

Use the refresh token to request a new one. RefreshTokenUsage = TokenUsage.OneTimeOnly, RefreshTokenExpiration = TokenExpiration.Sliding

gummadikalyan avatar Nov 20 '17 19:11 gummadikalyan

Sorry I don't understand. Why does the token expire after one day if I set it AccessTokenLifeTime to 10 days? If AccessTokenLifeTime doesn't decide the lifetime of the access token then its name is very confusing.

stt106 avatar Nov 20 '17 19:11 stt106

AccessTokenLifetime should be in seconds.

gummadikalyan avatar Nov 20 '17 19:11 gummadikalyan

I did set it to second e.g. 3600*24*10 seconds which is equal to 10 days.

stt106 avatar Nov 20 '17 19:11 stt106

Do a simple math and convert 10 day in to seconds and apply to AccessTokenLifetime please.

gummadikalyan avatar Nov 20 '17 20:11 gummadikalyan

Are you saying if I set it to 20*30 then it will only set it to 20 seconds instead of 600 seconds?

stt106 avatar Nov 20 '17 20:11 stt106

Yes, You need to do the exact math before assigning AccessTokenLifetime in seconds.

gummadikalyan avatar Nov 20 '17 20:11 gummadikalyan

I just tested. Setting TokenAccessLifeTime to 3600*24*10 does set the token expiration to 10 days so I am not sure why my supposedly unexpired access token doesn't work after 1 day...

stt106 avatar Nov 20 '17 20:11 stt106