Asp.net会话过期重定向到登录页面(Asp.net session expiry redirect to login page)

编程入门 行业动态 更新时间:2024-10-27 15:25:28
Asp.net会话过期重定向到登录页面(Asp.net session expiry redirect to login page)

会话过期时重定向到登录页面的最佳方式是什么? 我在用着

sessionState mode="InProc"

我可以在web.config文件中设置它吗?

What is the best way to redirect to the login page when the session expires. I'm using

sessionState mode="InProc"

Can I set this in the web.config file?

最满意答案

要记住会话过期的技巧是,这发生在幕后运行的工作进程中,并且没有直接的方式来通知用户而不必返回服务器来检查事情的状态。

我所做的是让页面注册一个Javascript块,在指定的超时后重新将用户重定向到登录页面:

Page.ClientScript.RegisterStartupScript(Me.GetType, "TimeoutScript", "setTimeout(""top.location.href = '~/Login.aspx'""," & ConfigurationManager.AppSettings("SessionTimeoutMilliseconds") & ");", True)

您会注意到我的web.config文件中存储了实际的毫秒数,因此我可以根据需要调整超时长度。

与Global.asax文件中典型的Session_End事件结合使用,可以在我的Web应用程序中处理会话超时的一种非常干净的方式。

The trick to remember about the session expiration is that this happens in the the worker process running behind the scenes and there is no direct way to notify the user without going back to the server to check the state of things.

What I do is I have the page register a Javascript block that will redirect the user to the login page again after the designated timeout:

Page.ClientScript.RegisterStartupScript(Me.GetType, "TimeoutScript", "setTimeout(""top.location.href = '~/Login.aspx'""," & ConfigurationManager.AppSettings("SessionTimeoutMilliseconds") & ");", True)

You'll notice that I have the actual milliseconds stored in my web.config file so that I can adjust the timeout length as needed.

Using this, combined with the typical Session_End event in the Global.asax file makes a pretty clean way of handling session timeouts in my web apps.

更多推荐

sessionState,mode,InProc,web,config,电脑培训,计算机培训,IT培训"/> <meta name

本文发布于:2023-08-02 21:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381545.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   页面   Asp   net   session

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!