Asp.net

MVC - 使用者必須在 IIS Recycle 上重新進行身份驗證

  • June 22, 2015

我需要一個應用程序池回收對我的網路應用程序的使用者完全透明。

目前,在 IIS 7 應用程序池回收後,所有登錄到我的 Web 應用程序的使用者都將被踢出並需要重新登錄(Context.User.Identity.IsAuthenticated 設置為 false)。我使用 SQL 狀態伺服器,我使用表單身份驗證,並且兩者都配置為使用 cookie。我的印像是 .NET 和/或 IIS 處理 cookie 的身份驗證。

但是,每次應用程序池被回收時 Context.User.Identity.IsAuthenticated 設置為 false (我不知道這發生在哪裡)我的使用者被踢出並需要重新登錄。我可以看到會話id 在整個登錄過程中保持不變,我還可以在數據庫/狀態伺服器中查看此會話資訊。

我不知道這是會話問題還是 cookie 問題。

請幫忙!

登錄方式:

public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl)
   {
       if (!ValidateLogOn(userName, password))
       {
           return View();
       }

       FormsAuth.SignIn(userName, true);  // uses FormsAuthentication.SetAuthCookie(username, true);
       Session["userName"] = userName;

       if (!String.IsNullOrEmpty(returnUrl))
       {
           return Redirect(returnUrl);
       }
       else
       {
           return RedirectToAction("Index", "Home");
       }
   }

自定義控制器屬性:

public class CookieAuthorizeAttribute : ActionFilterAttribute
{
   public override void OnActionExecuting(ActionExecutingContext filterContext)
   {
       HttpContext lvContext = HttpContext.Current;

           if (!lvContext.User.Identity.IsAuthenticated)
           {
               lvContext.Response.Redirect("~/Account/Logon");
           }
           else
           {
               FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
               FormsAuthentication.RenewTicketIfOld(identity.Ticket);
           }

       base.OnActionExecuting(filterContext);
   }
}

網路配置:

<authentication mode="Forms">
 <forms cookieless="UseCookies" loginUrl="~/Account/LogOn" slidingExpiration="true" name=".ASPXAUTH" requireSSL="false" timeout="2880" />
</authentication>

<modules runAllManagedModulesForAllRequests="true">
 <remove name="ScriptModule" />
 <remove name="UrlRoutingModule" />
 <remove name="Session" />
 <remove name="FormsAuthentication" />
 <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
 <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
 <add name="NHibernateMvcSessionModule" type="EpnNHibernateBase.NHibernateMvcSessionModule, EpnNHibernateBase" />
 <add name="Session" type="System.Web.SessionState.SessionStateModule" />
 <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
</modules>

我能夠自己找到解決方案。問題不在於如何以程式方式處理身份驗證或我如何對使用者進行身份驗證。問題在於我如何在 IIS/web.config 中配置身份驗證。

我仔細按照下面列出的連結中的步驟進行操作:

配置表單身份驗證 (IIS 7)(在每個相關部分進行分支)

在 IIS 7 中配置機器密鑰<–特別是這個

在密切遵循這些步驟之後,我能夠正確生成機器密鑰。本機鑰匙如下(帶假鑰匙):

&lt;machineKey decryptionKey="ASDF3WS545AS5D4F8254A12DAFA5SDF7,IsolateApps" validation="3DES" validationKey="A65A6S5DF46ASD4F89WEF6SAD2F4A68EF4AW65F4D3A2F4AS6DF89A98D4F6A5SD4F6A5SDF46ASD8F4A6S5DF46AS5D4F6AS5DF49AS8DF46AS5D4F6AS5DF46SAD5F,IsolateApps" /&gt;

此外,所需httpModules system.webServer:modules部分web.config還需要添加以下模組:

&lt;remove name="Session" /&gt;
&lt;remove name="FormsAuthentication" /&gt;
&lt;add name="Session" type="System.Web.SessionState.SessionStateModule" /&gt;
&lt;add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" /&gt;

摘要:據我所知,cookies 正在創建和加密,但由於沒有機器密鑰,我無法解密 cookie,因此需要重新驗證。

引用自:https://stackoverflow.com/questions/3960496