2012-03-16 141 views
6

我做了一個新的MVC3應用程序,它託管在WinHost的基本計劃上。登錄用戶在一段時間後註銷

問題的要點是,應用程序池內存限制已達到,並且每個會話InProc都被刪除,這意味着我的用戶已註銷。

按照他們的文檔,我看到:

http://support.winhost.com/KB/a626/how-to-enable-aspnet-sql-server-session-on-your-web.aspx

這裏是我的web.config的內容,按照上述建議的步驟後:

<?xml version="1.0"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=152368 
    --> 
<configuration> 
    <connectionStrings>  
    <!-- REMOVED FOR PRIVACY --> 
    </connectionStrings> 
    <appSettings> 
    <add key="webpages:Version" value="1.0.0.0"/> 
    <add key="ClientValidationEnabled" value="true"/> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
    </appSettings> 
    <system.web>  
    <sessionState mode="SQLServer" 
        allowCustomSqlDatabase="true"     
        cookieless="false" 
        timeout="2880" 
        sqlConnectionString="data Source='tcp:s407.winhost.com';database='DB_41_xx';user id='DB_11_xx_user'; password='xx';" /> 
    <trust level="Full"/> 
    <compilation debug="true" targetFramework="4.0"> 
     <assemblies> 
     <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     </assemblies> 
    </compilation> 
    <authentication mode="Forms"> 
     <forms loginUrl="~/" timeout="2880"/> 
    </authentication> 
    <membership> 
     <providers> 
     <clear/> 
     <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/> 
     </providers> 
    </membership> 
    <profile> 
     <providers> 
     <clear/> 
     <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/> 
     </providers> 
    </profile> 
    <roleManager enabled="false"> 
     <providers> 
     <clear/> 
     <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/"/> 
     <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/"/> 
     </providers> 
    </roleManager> 
    <pages> 
     <namespaces> 
     <add namespace="System.Web.Helpers"/> 
     <add namespace="System.Web.Mvc"/> 
     <add namespace="System.Web.Mvc.Ajax"/> 
     <add namespace="System.Web.Mvc.Html"/> 
     <add namespace="System.Web.Routing"/> 
     <add namespace="System.Web.WebPages"/> 
     </namespaces> 
    </pages> 
    </system.web> 
    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false"/> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/> 
     <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0"/> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/> 
     <bindingRedirect oldVersion="0.0.0.0-4.0.8.0" newVersion="4.0.8.0"/> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
</configuration> 

這裏躺着的問題:

我的用戶在一段時間後仍然記錄在案。我認爲使用SQL進行會話可以防止這個問題。

這裏的代碼對我怎樣我的洛用戶的相關位:

[HttpPost] 
public ActionResult Login(LogOnModel model) 
{ 
    using (EfAccountRepository accountRepository = new EfAccountRepository()) 
    { 
     if (accountRepository.ValidateCredentials(model.Email, model.Password)) 
     { 
      FormsAuthentication.SetAuthCookie(model.Email, true); 
      return RedirectToAction("Index", "Home"); 
     }  
    } 

    ModelState.AddModelError("", "Your email or password is incorrect."); 
    return View(model); 
} 

,這裏是一些代碼,我用它來看看用戶是否登錄:

public static MvcHtmlString AdminDashboardLink() 
    { 
     if (SecurityHelpers.UserIsPartOfCompany(HttpContext.Current)) 
     { 
      string html = "<li><a href='/Admin'>ADMIN DASHBOARD</a></li>"; 
      return new MvcHtmlString(html); 
     } 
     else 
     { 
      return new MvcHtmlString(""); 
     } 
    } 

    public static bool UserIsPartOfCompany(HttpContext context) 
    { 
     if (!context.Request.IsAuthenticated) 
      return false; 

     using (EfAccountRepository accountRepository = new EfAccountRepository()) 
     { 
      var loggedInUser = accountRepository.FindByEmail(context.User.Identity.Name); 
      string[] userRoles = accountRepository.GetRolesForUser(loggedInUser.AccountId); 

      return userRoles.Contains("Editor") || userRoles.Contains("Finance") || userRoles.Contains("Administrator"); 
     }    
    } 

有什麼建議嗎?也許我的web.config是拙劣的,這是導致問題。在我添加會話信息後,也許我還需要刪除某些內容?

+0

運行相同的瀏覽器會話時,您將看到用戶註銷發生? – 2012-03-16 01:58:55

+0

是的,我打開Firefox,登錄到網站(託管在Winhost上)。在同一個網站上玩一會兒,幾分鐘後(有時候幾秒鐘後),我就註銷了。 – 2012-03-16 02:08:32

+0

當一個asp.net應用程序池回收時,這將是空/空嗎? 'context.Request.IsAuthenticated'?如果是這樣,這可能是導致問題的原因。如果我切換到SQL會話,我是否需要修改檢查用戶是否登錄的方式? – 2012-03-16 02:10:32

回答

4

由於垃圾收集器清理分配給您的應用程序的機器密鑰並分配導致用戶註銷的新密鑰,因此會導致一些時間。解決方法是爲應用程序生成一個machineKey,並將其放置在web.config下的System.Web像

<system.web> 
    <machineKey validationKey="###YOUR KEY HERE ###" 
       decryptionKey="## decrypt key here ##" 
       validation="SHA1" decryption="AES" /> 
... 
... 

這個鏈接可以幫助你http://aspnetresources.com/tools/machineKey

+0

謝謝你的幫助。所以我生成了我的密鑰,並將其放在我的web.config中。我是否需要設置任何其他選項才能使其工作,還是隻需將它放在web.config中並完成? – 2012-03-16 14:24:00

+0

不會去考慮它,但它似乎只是將此添加到web.config文件,解決了它! :)將在兩個小時內確認;我仍然應該登錄。 – 2012-03-16 14:42:51

+0

只是把它放在配置會做......';)' – Rafay 2012-03-16 16:23:31

-1

表單驗證與會話無關。它與會話狀態無關。所需的一切都存儲在表單auth cookie中。

您的超時設置爲2880,因此48小時即兩天,所以我期望超時發生。

+0

好的,所以我在用戶中登錄的方式並不正確,應該可以正常工作。另一方面,我也希望發生超時 - 但目前的超時時間不會接近兩天,更不用說兩個小時。任何其他想法? – 2012-03-16 01:49:09

+0

我正在使用'HttpContext.Request.IsAuthenticated'來查看用戶是否已登錄。這是否與SQL會話很好地相同,它可以在InProc會話(默認爲MVC3)上正常工作? – 2012-03-16 01:50:18

+0

實際上,您可以解密一張票並檢查它(並記錄它),以便更好地處理髮生的事情:http://www.hanselman.com/blog/AccessingTheASPNETFormsAuthenticationTimeoutValue。aspx是否確定您的應用程序中沒有其他會話檢查?有些人嘗試同步(或假設他們是同步,他們不是)會話並形成auth超時,但他們不會保持彼此同步。如果你的應用程序的其他地方有代碼重定向到登錄,如果會話爲空,這可能是你的問題的另一個來源。 – 2012-03-16 03:05:42