gpt4 book ai didi

c# - 在 ASP.net 中处理表单例份验证超时

转载 作者:太空宇宙 更新时间:2023-11-03 11:04:12 25 4
gpt4 key购买 nike

我可以在 Global.asax 中处理表单例份验证超时吗?就像global.asax中的Session_End一样?请指教。

我在我的 webconfig 中设置表单验证超时:

<forms name="formName" loginUrl="Login.aspx" protection="All" path="/" timeout="30"/>

感谢大家! :)

最佳答案

不可以,因为超时是在身份验证 cookie 上编码的,并且存在于浏览器中(而不是服务器端)。

您可以进行自定义,也可以在数据库中保留用户超时 - 但这并不容易,您可以使用 global.asax 上的 Application_AuthenticateRequest 在请求之前进行检查如果用户不再经过身份验证。

一个关于如何在用户未通过身份验证的情况下删除 session 数据的示例。在全局 asax 上。

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
// get the authCookie
HttpCookie authCookie = Context.Request.Cookies[cookieName];
// if is null then the use is not Authendicated
if (null == authCookie && System.Web.HttpContext.Current.Session != null)
{
// now check if you have Session variables that you wish to remove.
if(System.Web.HttpContext.Current.Session["flag"] == "1")
{
// remove your session data


}
}
}

你也可以检查

if(HttpContext.Current.User == null || HttpContext.Current.User.Identity == null || !HttpContext.Current.User.Identity.IsAuthenticated)
{
// now check if you have Session variables that you wish to remove.
if(Session["flag"] == "1")
{
// remove your session data

}
}

关于c# - 在 ASP.net 中处理表单例份验证超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16535675/

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