MVC 5认证用户注销,没有“记住我”;(ExpireTimeSpan / MachineKey / Session T
本文关键字:记住我 ExpireTimeSpan MachineKey Session 用户注销 没有 MVC 认证 | 更新日期: 2023-09-27 18:11:23
前言:我读过这本(ASP。. NET MVC成员——用户经常注销——不知道为什么)。. NET身份2记住我-用户正在注销)。. NET 5 Identity 3用户在一段时间后被注销)和这个(User gets logged out with 'Remember me')
我在这儿做噩梦了。我在VS 15中设置了一个锅炉板MVC5应用程序。一切都已更新到最新(特别是身份。核心和身份。(因为我读到有一个问题与锅炉板MS的东西)。
我们注意到用户在我们的一个应用程序上大约10-15分钟不活动后就会注销。此外,我们的用户报告说,"记住我"功能根本不起作用。
我不知道我做错了什么…
Startup.Auth.cs:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
SlidingExpiration = true,
ExpireTimeSpan = System.TimeSpan.FromDays(30.0),
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
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>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
我的配置文件:
<system.web>
<httpCookies httpOnlyCookies="true" requireSSL="true" lockItem="true" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" slidingExpiration="true" timeout="60" requireSSL="true" />
</authentication>
<compilation targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<machineKey validationKey="AutoGenerate,IsolateApps" decryptionKey="AutoGenerate,IsolateApps" validation="SHA1" decryption="Auto" />
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
Authentication in AccountController:
[ValidateAntiForgeryToken]
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
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.Username, model.Password, isPersistent: true, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
我基本上是想给用户一个30滑动cookie。也就是说,只要他们在30天内访问网站并且没有清除他们的cookie,他们就不必重新认证。
如您所见,isPersistent在登录时被设置为true,因此用户应该始终被有效地视为memorme = true;
是有什么明显的我错过了这里,或者我完全不理解是如何持久和永久登录工作?
如果我做了什么蠢事,请随便起哄。提前谢谢。
注意:我已经修改了配置文件中表单认证项的超时值,从我从测试中看到的没有变化。
编辑:我和我的托管公司谈过了,他们在15分钟的闲置后回收了应用程序池。他们现在已经延长了这一期限。然而,这并不能解决问题。
我认为拥有机器密钥可以确保cookie存活和应用程序回收?我已经输入了一个明确的机器钥匙,看看效果如何。
所以出于某种原因(仍然不确定是什么),我的摆弄似乎起作用了。
我最终不得不生成一个显式的Machine key而不使用
validationKey="AutoGenerate,IsolateApps"
decryptionKey="AutoGenerate,IsolateApps"
我还要求我的托管公司延长IIS应用程序池空闲回收时间(不确定这是正确的名称,但这是托管支持人员所说的)。
这似乎已经解决了它,测试了一夜,我没有登出。还是有点困惑。根据托管公司的说法,应用程序池仍在回收,尽管间隔时间更长。
我现在认为显式机器键可能与它有关?但我以前用过"AutoGenerate,IsolateApps",它工作得很好。
我希望这能帮助到别人。我真的还没有时间去做全面的测试,拆下机器钥匙看看它是否还能工作。一旦我完成了一些确定的测试,我会向你报告的。