2016-10-03 64 views
1

前言:我讀過這個(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能夠存活和應用程序回收?我現在已經放入了一個明確的機器密鑰,並看看它是如何發展的。仍然有點難住的問題,以及如何解決它...

+0

你主持什麼? – Henry24

+0

共享主機,是的,我剛剛閱讀了有關應用程序池回收的評論。我玩過了代碼,並確認我已註銷,即使cookie似乎仍然存在且有效。 – DarthJam

+0

檢查會話狀態,如果會話過期,用戶可以註銷 – Henry24

回答

1

因此,由於某種原因(仍不知道是什麼)我的小提琴似乎工作。

我最後不得不產生一個明確的機鍵,而不是使用

validationKey="AutoGenerate,IsolateApps" 
decryptionKey="AutoGenerate,IsolateApps" 

我也問我的託管公司,以擴展IIS應用程序池空閒循環時間(不知道這是正確的名稱,但是那是什麼託管支持人員稱它)。

這似乎已經解決了它,並在晚上測試我沒有註銷。仍然有點難倒。應用程序池仍在根據託管公司進行回收,儘管時間間隔較長。

我現在正在考慮明確的機器密鑰可能與它有關嗎?但之前我使用過「AutoGenerate,IsolateApps」,並且它工作正常。

我希望這可以幫助別人。真的沒有時間去做完整的測試,並刪除機器的鑰匙,看它是否仍然有效。一旦我可以建立一些明確的測試,我會報告回來。

+1

將您的驗證密鑰設置爲自動生成將導致應用程序在每次啓動時(通過應用程序池回收或IIS非活動狀態)創建一個新的隨機密鑰。設置一個明確的密鑰將確保cookie將保持應用程序重新啓動和(如果有的話)負載平衡。 –

+0

這絕對是答案 - 創建一個,所以我可以upvote! @ Zhaph-BenDuguid – DarthJam