MVC - 用户必须在IIS Recycle上重新进行身份验证

ala*_*lan 14 asp.net asp.net-mvc session-state

我需要一个应用程序池回收对我的Web应用程序的用户完全透明.

目前,在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");
        }
    }
Run Code Online (Sandbox Code Playgroud)

自定义控制器属性

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

WebConfig:

<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>
Run Code Online (Sandbox Code Playgroud)

ala*_*lan 17

我能够自己找到解决方案.问题不在于如何以编程方式处理身份验证或如何对用户进行身份验证.问题在于我如何在IIS/web.config中配置身份验证.

我仔细地按照下面链接中的步骤操作:

配置表单身份验证(IIS 7)(在每个相关部分分支)

在IIS 7中配置机器密钥 < - 特别是这一个

在密切关注这些步骤后,我能够正确生成机器密钥.该机器钥匙如下(带有制造的钥匙):

<machineKey decryptionKey="ASDF3WS545AS5D4F8254A12DAFA5SDF7,IsolateApps" validation="3DES" validationKey="A65A6S5DF46ASD4F89WEF6SAD2F4A68EF4AW65F4D3A2F4AS6DF89A98D4F6A5SD4F6A5SDF46ASD8F4A6S5DF46AS5D4F6AS5DF49AS8DF46AS5D4F6AS5DF46SAD5F,IsolateApps" />
Run Code Online (Sandbox Code Playgroud)

另外,httpModules system.webServer:modules部分在web.config需要加入以下模块:

<remove name="Session" />
<remove name="FormsAuthentication" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
Run Code Online (Sandbox Code Playgroud)

简介:从我收集的内容中,cookie被创建和加密,但由于没有机器密钥,我无法解密cookie,因此需要重新进行身份验证.

  • 如果未指定计算机密钥,则在池启动时会自动生成特定于应用程序池的随机密钥.当池被回收时,密钥将被重新生成,使所有现有的cookie显示无效. (8认同)