It seems I don\'t understand clearly how IsPersistent
in OWIN cookie authentication works, the code below is to use IsPersistent
:
v
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;
}
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.
Persistent cookies for .Net Core 2.2 you might want to try this
Example
var claimsIdentity = new ClaimsIdentity(new[]
{ new Claim(ClaimTypes.Name, "test"),},CookieAuthenticationDefaults.AuthenticationScheme);
HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTime.UtcNow.AddMinutes(30)
});