Asp.net Can';添加“配置应用程序cookie”后无法登录
我正在用Identity开发Asp.Net Core 3.1 MVC web应用程序。我的登录页面工作正常,但在添加了Asp.net Can';添加“配置应用程序cookie”后无法登录,asp.net,.net,asp.net-mvc,asp.net-core,cookies,Asp.net,.net,Asp.net Mvc,Asp.net Core,Cookies,我正在用Identity开发Asp.Net Core 3.1 MVC web应用程序。我的登录页面工作正常,但在添加了服务之后。ConfigureApplicationOkie没有按预期工作,它不断将我重定向到本地索引。我不知道到底发生了什么。如果我对这个块进行注释,一切都会按预期进行 这是我在启动文件中的ConfigureServices方法 // This method gets called by the runtime. Use this method to add services
服务之后。ConfigureApplicationOkie
没有按预期工作,它不断将我重定向到本地索引。我不知道到底发生了什么。如果我对这个块进行注释,一切都会按预期进行
这是我在启动文件中的ConfigureServices方法
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<IdentityUser, IdentityRole>(options => {
options.SignIn.RequireConfirmedAccount = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddIdentityCore<ApplicationUser>()
.AddRoles<IdentityRole>()
.AddClaimsPrincipalFactory<UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>>()
.AddEntityFrameworkStores<ApplicationDbContext>()
//.AddDefaultTokenProviders()
.AddDefaultUI();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Name = ".ExpirationCookie";
options.Cookie.IsEssential = true;
options.Cookie.HttpOnly = true;
options.LoginPath = "/Identity/Pages/Account/Login";
options.AccessDeniedPath = "/Identity/Pages/Account/AccessDenied";
options.ExpireTimeSpan = TimeSpan.Zero;
options.SlidingExpiration = true;
options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
options.Events = new CookieAuthenticationEvents
{
OnValidatePrincipal = ValidateAsync.ValidatingAsync
};
})
.Configure<SecurityStampValidatorOptions>(options =>
{
options.ValidationInterval = TimeSpan.Zero;
});
services.AddSingleton<IEmailSender, EmailSender>();
services.Configure<EmailOptions>(Configuration);
services.AddHangfire(config => config.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")));
services.AddHangfireServer();
services.AddControllersWithViews(); //?
services.AddRazorPages().AddRazorRuntimeCompilation(); //?
services.AddScoped<IExpirationJob, ExpirationJob>();
services.AddScoped<IReminderJob, EmailReminder>();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.Configure<IdentityOptions>(options =>
{
// Password settings.
//options.Password.RequireDigit = true;
//options.Password.RequireLowercase = true;
//options.Password.RequireNonAlphanumeric = true;
//options.Password.RequireUppercase = true;
//options.Password.RequiredLength = 6;
//options.Password.RequiredUniqueChars = 1;
// Lockout settings.
//options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
//options.Lockout.MaxFailedAccessAttempts = 5;
//options.Lockout.AllowedForNewUsers = true;
// User settings.
//options.User.AllowedUserNameCharacters =
// "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+ ";
//options.User.RequireUniqueEmail = false;
});
}
//此方法由运行时调用。使用此方法向容器中添加服务。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“DefaultConnection”);
服务.附加性(选项=>{
options.SignIn.RequireConfirmedAccount=false;
})
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddIdentityCore()
.AddRoles()
.AddClaimsPrincipalFactory()
.AddEntityFrameworkStores()
//.AddDefaultTokenProviders()
.AddDefaultUI();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
services.configureApplicationOK(选项=>
{
options.Cookie.Name=“.ExpirationCookie”;
options.Cookie.IsEssential=true;
options.Cookie.HttpOnly=true;
options.LoginPath=“/Identity/Pages/Account/Login”;
options.AccessDeniedPath=“/Identity/Pages/Account/AccessDeniedPath”;
options.ExpireTimeSpan=TimeSpan.Zero;
options.SlidingExpiration=true;
options.ReturnUrlParameter=CookieAuthenticationDefaults.ReturnUrlParameter;
options.Events=新建CookieAuthenticationEvents
{
OnValidatePrincipal=ValidateAsync.ValidatingAsync
};
})
.Configure(选项=>
{
options.ValidationInterval=TimeSpan.Zero;
});
services.AddSingleton();
服务。配置(配置);
services.AddHangfire(config=>config.UseSqlServerStorage(Configuration.GetConnectionString(“DefaultConnection”));
services.AddHangfireServer();
services.AddControllersWithViews();/?
services.AddRazorPages().AddRazorRuntimeCompilation();/?
services.addScope();
services.addScope();
services.AddSingleton();
配置(选项=>
{
//密码设置。
//options.Password.RequireDigit=true;
//options.Password.RequireLowercase=true;
//options.Password.RequireNonAlphanumeric=true;
//options.Password.RequireUppercase=true;
//options.Password.RequiredLength=6;
//options.Password.RequiredUniqueChars=1;
//锁定设置。
//options.Lockout.DefaultLockoutTimeSpan=TimeSpan.FromMinutes(5);
//options.locket.MaxFailedAccessAttempts=5;
//options.locket.AllowedForNewUsers=true;
//用户设置。
//options.User.AllowedUserName字符=
//“ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ0123456789-.@+”;
//options.User.RequireUniqueEmail=false;
});
}
来自:
控制cookie从创建时起保持有效的时间。过期信息在受保护的cookie票据中。因此,过期的cookie将被忽略,即使它在浏览器应该清除它之后被传递到服务器
在ConfigureApplicationCookie
中,将cookie寿命设置为0
Cookie身份验证将永远不会成功,您需要更改它,例如从秒开始,从分钟开始,从小时开始
//......
options.ExpireTimeSpan = TimeSpan.FromHours(24);
//.......