Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Cookie expiration time configured in ConfigureApplicationCookie ignored by Identity #1425

Closed
B-Esmaili opened this issue Sep 12, 2017 · 9 comments

Comments

@B-Esmaili
Copy link

B-Esmaili commented Sep 12, 2017

Here is my configuration for identity cookie :

services.ConfigureApplicationCookie(cfg =>
            {
                cfg.Cookie.Name = "application_ms_state";
                cfg.Cookie.Expiration = TimeSpan.FromDays(15);
                cfg.SlidingExpiration = true;
            });

cookie

as you can see cookies is generated for 15 days but it expires in almost half an hour.could anyone clarify what is problem with my configuration?

@VahidN
Copy link

VahidN commented Sep 12, 2017

Have you configured your server to store the cookie decryption keys permanently? if not, whenever the server restarts or the app pool restarts, your users will have to login again.
More info

@B-Esmaili
Copy link
Author

I went through the documentation and did as mentioned but still i get logged out after 30minutes.

@natemcmaster
Copy link
Contributor

natemcmaster commented Sep 13, 2017

Can you try changing your code to this?

cfg.ExpireTimeSpan = TimeSpan.FromDays(15);

As mentioned in the xmldocs, Cookie.Expiration is ignored on CookieAuthenticationOptions.

https://github.com/aspnet/Security/blob/a53bf093a7d86b35e019c80515c92d7626982325/src/Microsoft.AspNetCore.Authentication.Cookies/CookieAuthenticationOptions.cs#L62

Possible duplicate of https://github.com/aspnet/Security/issues/1293

@B-Esmaili
Copy link
Author

I have done this as well but no luck.@natemcmaster could you give me a reference to code which checks for ticked expiration?

@B-Esmaili
Copy link
Author

B-Esmaili commented Sep 14, 2017

I found the location in which check for expiration is done and figured out that the problem is ,identity checks every 30 minutes (by default, its configurable) to see if issued authentication ticked is valid, consequently it checks to see if the class which is implementing UserStore<> is implementing IUserSecurityStampStore (UserManager.SupportsUserSecurityStamp in following code) for that matter.

if (user != null && UserManager.SupportsUserSecurityStamp)

and following is getter of SupportsUserSecurityStamp

return Store is IUserSecurityStampStore<TUser>;

As my MembershipService class which is implementing IUserStroe does not implement IUserSecurityStampStore<> thus after 30 min interval i end up with invalid security stamp and a null principal which == SignOut. this was my bad story.
You can close this issue as my problem is resolved.

@natemcmaster
Copy link
Contributor

Ok, thanks for letting us know @B-Esmaili. We'll use aspnet/Security#1293 for further follow up on the Cookie.Expiration vs ExpireTimeSpan discussion.

@tiljanssen
Copy link

I had the same problem. It took me quite some time to find out the cause.

Shouldn't addIdentity() check whether the securityStampValidatorStore is supported, and if not, either throw or skip the registration of the validation?

@HaoK
Copy link
Member

HaoK commented Jul 16, 2018

Sure that's a reasonable suggestion, can you file a new issue asking for that improvement? its something we can consider for 2.2 as it would be very cheap @tiljanssen

@aguayUmbt
Copy link

asd
I would like to extend my authentication duration to 1 day, but from what I've understood, it's logging out after 30minutes. If I set it to 1 minutes, it actually work.
I've consulted many topics regarding this matter but couldn't find an answer I could understand. Please help me with this, thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants