简体   繁体   English

ASP.Net核心身份与分布式会话

[英]ASP.Net Core Identity with distributed session

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. 我在我的ASP.NET应用程序中使用Identity,但在AWS上运行两个Web服务器,并且遇到的问题是,当您在一台服务器上登录时,您没有登录另一台服务器,因为它们不共享同一会议。

This is also a problem eg 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: 我尝试在我的Configure方法中使用Redis添加分布式缓存:

app.UseSession();

And my ConfigureServices : 和我的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: 首先,我将cookie选项设置为具有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 此外,在使用外部登录的时候,我的设置isPersistent的说法_signInManager.ExternalLoginSignInAsynctrue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM