gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:41:32 25 4
gpt4 key购买 nike

我需要一个应用程序池回收对我的网络应用程序的用户完全透明。

目前,在 IIS 7 应用程序池回收后,所有登录到我的 Web 应用程序的用户都将被踢出并需要重新登录(Context.User.Identity.IsAuthenticated 设置为 false)。我使用 SQL 状态服务器,我使用表单例份验证,并且两者都配置为使用 cookie。我的印象是 .NET 和/或 IIS 处理 cookie 的身份验证。

但是,每次应用程序池被回收时 Context.User.Identity.IsAuthenticated 设置为 false (我不知道这发生在哪里)我的用户被踢出并需要重新登录。我可以看到 session id 在整个登录过程中保持不变,我还可以在数据库/状态服务器中查看此 session 信息。

我不知道这是 session 问题还是 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");
}
}

自定义 Controller 属性:
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 中配置身份验证。

我仔细按照下面列出的链接中的步骤进行操作:

Configuring Forms Authentication (IIS 7) (在每个相关部分上进行分支)

Configuring Machine Keys in IIS 7 <-- 特别是这个

在密切遵循这些步骤之后,我能够正确生成机器 key 。
native key 如下(带假 key ):

<machineKey decryptionKey="ASDF3WS545AS5D4F8254A12DAFA5SDF7,IsolateApps" validation="3DES" validationKey="A65A6S5DF46ASD4F89WEF6SAD2F4A68EF4AW65F4D3A2F4AS6DF89A98D4F6A5SD4F6A5SDF46ASD8F4A6S5DF46AS5D4F6AS5DF49AS8DF46AS5D4F6AS5DF46SAD5F,IsolateApps" />

此外, 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" />

摘要:据我所知,cookies 正在创建和加密,但由于没有机器 key ,我无法解密 cookie,因此需要重新验证。

关于asp.net - MVC - 用户必须在 IIS Recycle 上重新进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3960496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com