Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How IsPersistent works in OWIN Cookie authentication

It seems I don't understand clearly how IsPersistent in OWIN cookie authentication works, the code below is to use IsPersistent:

var context = Request.GetOwinContext(); var authManager = context.Authentication; var properties = new AuthenticationProperties { IsPersistent = isPersistence };  authManager.SignIn(properties, identity); 

I don't see the difference when user checks/unchecks Remember me (uses IsPersistent behind) because if I close Chrome browser and open it again to go with the website, the cookie .AspNet.ApplicationCookie is still there and it lets me in even I check or uncheck Remember me.

I have checked the definition of IsPersistent on the link:

Gets or sets whether the authentication session is persisted across multiple requests.

But don't get much understanding since I see it still works.

The code to setup OWIN cookie authentication:

app.UseCookieAuthentication(new CookieAuthenticationOptions {     AuthenticationMode = AuthenticationMode.Active,     AuthenticationType = ApplicationTypes.ApplicationCookie,     ExpireTimeSpan = TimeSpan.FromMinutes(30),     LoginPath = new PathString("/Account/LogOn") }); 
like image 557
cuongle Avatar asked Aug 11 '15 15:08

cuongle


People also ask

Does ASP.NET Core identity use cookies?

ASP.NET Core provides a cookie authentication mechanism which on login serializes the user details in form of claims into an encrypted cookie and then sends this cookie back to the server on subsequent requests which gets validated to recreate the user object from claims and sets this user object in the HttpContext so ...

What is cookie based authentication in C#?

The entire cookie-based authentication works in the following manner: The user gives a username and password at the time of login. Once the user fills in the login form, the browser (client) sends a login request to the server. The server verifies the user by querying the user data.

How do I enable cookies in ASP.NET Core?

For a general cookie manually created within your application, you control the flags for security when creating it - for example: Response. Cookies. Append( "COOKIE_NAME", "COOKIE_VALUE", new CookieOptions() { Path = "/", HttpOnly = false, Secure = false } );


2 Answers

Persistent cookies will be saved as files in the browser folders until they either expire or manually deleted. This will cause the cookie to persist even if you close the browser.

If IsPersistent is set to false, the browser will acquire session cookie which gets cleared when the browser is closed.

Now the reason session cookie wont clear after restarting the browser is because of chrome default settings. To fix it go to chrome settings -> advanced, and uncheck Continue running background apps when Google Chrome is closed under System section.

like image 84
Hezye Avatar answered Oct 08 '22 19:10

Hezye


public void Configuration(IAppBuilder app) {     //Some Code     app.UseCookieAuthentication(GetCookieAuthenticationOptions());     //Some Code }  private static CookieAuthenticationOptions GetCookieAuthenticationOptions() {     var options  = new CookieAuthenticationOptions();     {         CookieName = "AuthCookie",  //Some cookie settings here     };     var provider = (CookieAuthenticationProvider)options.Provider;     provider.OnResponseSignIn = (context) =>      {         context.Properties.IsPersistent = true;         context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddHours(24);     };     return options; } 
like image 23
Balaji Gunasekaran Avatar answered Oct 08 '22 17:10

Balaji Gunasekaran