我在10台服务器之间平衡了站点负载。看来,当用户在服务器之间切换时,他们必须重新登录。我将机器密钥和会话状态存储在Redis中。 .AspNetCore.Identity.Application cookie存在,并且在注销时不会过期。似乎在一台服务器上生成的cookie在另一台服务器上未被接受。我需要为负载均衡设置另一个设置吗?
这是我的ConfigureServices代码。
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddIdentity<Implementation.Core.IdentityUser, Implementation.Core.IdentityRole>()
.AddUserStore<Implementation.UserStore<Implementation.Core.IdentityUser,
Implementation.Core.IdentityRole,
Implementation.Core.UserClaim,
Implementation.Core.UserLogin,
Implementation.Core.UserRole>>()
.AddRoleStore<Implementation.RoleStore<Implementation.Core.IdentityRole>>();
services.AddScoped<Microsoft.AspNetCore.Identity.IPasswordHasher<Implementation.Core.IdentityUser>, Implementation.PasswordHasher>();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Login";
options.ReturnUrlParameter = "RedirectUrl";
options.LogoutPath = "/Login/Logout";
options.AccessDeniedPath = "/Login/AccessDenied";
});
services.ConfigureApplicationCookie(o =>
{
o.ExpireTimeSpan = TimeSpan.FromMinutes(60);
o.SlidingExpiration = true;
});
// Add framework services.
services.AddMvc()
.AddApplicationPart(typeof(Controllers.LocationController).Assembly)
.AddApplicationPart(typeof(SysAdmin.Controllers.CommandController).Assembly)
.AddApplicationPart(typeof(Supplier.Controllers.SupplierLocationController).Assembly)
.AddApplicationPart(typeof(SpectacleCatalog.Controllers.LensDesignController).Assembly)
.AddApplicationPart(typeof(Account.Controllers.CBUSearchController).Assembly);
//services.AddDistributedMemoryCache();
OutOfStateSettings outOfState = new OutOfStateSettings();
Configuration.Bind("OutOfState", outOfState);
services.AddDistributedRedisCache(opts =>
{
opts.Configuration = outOfState.RedisIP;
opts.InstanceName = outOfState.CacheId;
});
var redis = ConnectionMultiplexer.Connect($"{outOfState.RedisIP}:{outOfState.RedisPort}");
services.AddDataProtection().PersistKeysToRedis(redis, outOfState.MachineKeyId);
services.AddOptions();
services.AddSingleton<
Microsoft.AspNetCore.Mvc.DataAnnotations.IValidationAttributeAdapterProvider,
Util.CustomValidatiomAttributeAdapterProvider>();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(120);
});
var builder = new ContainerBuilder();
builder.Populate(services);
this.ApplicationContainer = builder.Build();
return new AutofacServiceProvider(this.ApplicationContainer);
}
答案 0 :(得分:0)
如果您使用相同的机器密钥,则它应该可以正常工作。 您正在使用多少台Redis服务器?全部都在集群中? 某些Redis服务器群集设置不正确可能会发生。
但是,如果需要可伸缩性,通常应尝试避免会话。您可以使用声明来存储某些信息,而不是使用会话
答案 1 :(得分:0)
我前段时间也遇到过类似的问题。事实证明,我使用的是基于内存的缓存(特定于计算机)作为主缓存,而Redis是辅助缓存。因此密钥有时仅缓存在内存中。
因此,只需确认它与您的情况不同即可。
幸运的是!