前言:我讀過這個(ASP.NET MVC membership - user being logged out frequently - don't know why),這(ASP.NET Identity 2 Remember Me - User Is Being Logged Out),這(ASP.NET 5 Identity 3 users get signed out after some time),這(User gets logged out with 'Remember me')。MVC 5認證的用戶,沒有 「記住我」(ExpireTimeSpan/machineKey中/會話超時)
我在這裏有點噩夢。我在VS 15中設置了一個鍋爐板MVC5應用程序。所有內容都已更新到最新版本(特別是identity.core和identity.owin,因爲我讀到了鍋爐板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>
認證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,所以用戶應該總是被視爲像rememberMe = true;
有沒有什麼明顯的我在這裏錯過了,或者我完全不理解持久性和永久登錄是如何工作的?
如果我做了一些愚蠢的事情,請隨時嘲笑。提前致謝。
注意:我已經修改了配置文件中表單身份驗證條目的超時值,沒有從測試中看到的更改。
編輯:我對我的託管公司說,他們在閒置15分鐘後回收應用程序池。他們現在已經延伸了這個。但是,這並不能解決問題。
我以爲有了機器鑰匙可以確保cookie能夠存活和應用程序回收?我現在已經放入了一個明確的機器密鑰,並看看它是如何發展的。仍然有點難住的問題,以及如何解決它...
你主持什麼? – Henry24
共享主機,是的,我剛剛閱讀了有關應用程序池回收的評論。我玩過了代碼,並確認我已註銷,即使cookie似乎仍然存在且有效。 – DarthJam
檢查會話狀態,如果會話過期,用戶可以註銷 – Henry24