I'm using Identity in my ASP.NET application, but running on two webservers on AWS, and have the problem that when you're logged in on one server, you are not logged in on the other because they don't share the same session.
This is also a problem e.g. when I'm deploying a new version of my application - then the users will be logged out and have to log in again.
I have tried adding a distributed cache with Redis, in my Configure
method:
app.UseSession();
And my ConfigureServices
:
services.AddDistributedRedisCache(options =>
{
options.Configuration = "uri-to-redis-server";
options.InstanceName = "name";
});
services.AddIdentity<ApplicationUser, IdentityRole>(o =>
{
o.Password.RequireNonAlphanumeric = false;
})
.AddEntityFrameworkStores<MyContext>()
.AddDefaultTokenProviders();
services.AddMvc();
services.AddSession();
Any ideas on what I can do?
I fixed it! First, I set the cookie options to have an ExpireTimeSpan:
services.AddIdentity<ApplicationUser, IdentityRole>(o =>
{
o.Password.RequireNonAlphanumeric = false;
o.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(24);
o.Cookies.ApplicationCookie.SlidingExpiration = true;
})
.AddEntityFrameworkStores<MyDbContext>()
.AddDefaultTokenProviders();
Furthermore, when using external login, I set the isPersistent
argument of _signInManager.ExternalLoginSignInAsync
to true