I'm developing an API in asp.net core 2.0 with EF core. I have configured identity cookie authentication in startup.cs
as follows.
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.Cookie.Name = "example-cookie-name";
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
options.Cookie.SameSite = SameSiteMode.None;
options.ExpireTimeSpan = TimeSpan.FromDays(14);
options.SlidingExpiration = true;
options.Cookie.Expiration = TimeSpan.FromDays(14);
});
When a user log in to the api, it creates a cookie setting the expiry time as 1969-12-31T23:59:59.000Z
(extracted from Chrome brower F12). After some time(20-30)min later, all the claims are no longer exists.
Also, User.Identity.IsAuthenticated
has set to false
. Because of that, I'm not able to resolve the database context(utilizing claim values).
Is this the expected behavior in asp.net identity core? If so, how can I use a middleware solution to delete the cookie or some other workaround to do that? because once the cookie is deleted user can re-login.
Content updated
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<DataContext>()
.AddDefaultTokenProviders();
Also in
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseAuthentication();
app.UseContextAuthorizeValidatorMiddleware();
app.UseMvc();
}
Inside UseContextAuthorizeValidatorMiddleware
, I checked this,
public Task Invoke(HttpContext httpContext)
{
var isAuthenticated = httpContext.User.Identity.IsAuthenticated;
//...
return _next(httpContext);
}