C# 用户使用';记住我';
我似乎在理解Identity 2.0和Cookie的工作方式方面有些困难。NETMVC5 我想要的是: 如果用户登录并选中“记住我”复选框,我不希望他永远注销。。但实际情况是:用户在某个时间间隔后注销 如果用户在时间跨度之前关闭浏览器,“记住我”功能将起作用。(当他重新打开网站时,他仍在登录。) 这是我用于登录的代码:C# 用户使用';记住我';,c#,asp.net-mvc,cookies,asp.net-identity-2,C#,Asp.net Mvc,Cookies,Asp.net Identity 2,我似乎在理解Identity 2.0和Cookie的工作方式方面有些困难。NETMVC5 我想要的是: 如果用户登录并选中“记住我”复选框,我不希望他永远注销。。但实际情况是:用户在某个时间间隔后注销 如果用户在时间跨度之前关闭浏览器,“记住我”功能将起作用。(当他重新打开网站时,他仍在登录。) 这是我用于登录的代码: public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) {
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// Require the user to have confirmed their email before they can log on.
var user = await UserManager.FindByNameAsync(model.Email);
if (user != null)
{
if (!await UserManager.IsEmailConfirmedAsync(user.Id))
{
await SendEmailConfirmationTokenAsync(user.Id);
ModelState.AddModelError("", "Gelieve eerst je e-mailadres te bevestigen.");
return View(model);
}
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Ongeldige aanmeldpoging.");
return View(model);
}
}
公共异步任务登录(LoginViewModel模型,字符串返回URL)
{
如果(!ModelState.IsValid)
{
返回视图(模型);
}
//要求用户在登录之前确认其电子邮件。
var user=await UserManager.FindByNameAsync(model.Email);
如果(用户!=null)
{
如果(!wait UserManager.IsEmailConfirmedAsync(user.Id))
{
等待SendEmailConfirmationTokenAsync(user.Id);
AddModelError(“,“Gelieve eerst je e-mailadres te BEVSTIGEN”);
返回视图(模型);
}
}
//这不会将登录失败计入帐户锁定
//要使密码失败触发帐户锁定,请更改为shouldLockout:true
var result=wait-SignInManager.PasswordSignInAsync(model.Email、model.Password、model.RememberMe、shouldllockout:true);
开关(结果)
{
案例标志状态成功:
返回重定向到本地(returnUrl);
案例标志状态锁定输出:
返回视图(“锁定”);
案例信号状态故障:
违约:
AddModelError(“,“Ongeldige aanmeldpoging.”);
返回视图(模型);
}
}
这是Startup.Auth中的代码:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
ExpireTimeSpan = TimeSpan.FromMinutes(5),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(
validateInterval: TimeSpan.FromMinutes(10),
regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
getUserIdCallback: (id) => (id.GetUserId<int>()))
}
});
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
ExpireTimeSpan=从分钟(5)开始的时间跨度,
Provider=新CookieAuthenticationProvider
{
//允许应用程序在用户登录时验证安全戳。
//这是一种安全功能,在您更改密码或向帐户添加外部登录时使用。
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromMinutes(10),
regenerateIdentityCallback:(管理器,用户)=>user.GenerateUserIdentityAsync(管理器),
getUserIdCallback:(id)=>(id.GetUserId())
}
});
因此,我希望用户在5分钟后不会注销,因为在PasswordSignInAsync函数中设置了isPersistent标志
谢谢你的帮助。这是一个很好的例子
可以通过使用您自己的代码替换SecurityStampValidator.OnValidateIdentity
来修复此问题-重新生成cookie时,它会忘记在新cookie中添加“RememberMe”属性,从而使新cookie不会持久化
我认为这已经在v2.2中解决了,但是这个版本还没有投入生产。遗憾的是,我现在找不到这方面的原始错误报告