Asp.net会话过期重定向到登录页面重定向、页面、Asp、net

2023-09-03 01:18:25 作者:暴晒在一旁的寂寞っ

什么是重定向到登录页面时,会话过期的最佳方法。我使用的是

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

sessionState mode="InProc"

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

Can I set this in the web.config file?

推荐答案

要记住会话过期的诀窍是,这发生在运行幕后的工作进程并没有直接的方法,而无需通知用户返回到服务器,检查事物的状态。

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.

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

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)

您会发现,我已经保存在我的w​​eb.config文件中的实际毫秒,这样的需要,我可以调整超时长度。

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.

利用这一点,再加上在Global.asax文件中的典型Session_End中的事件作出处理会话超时在我的web应用程序一个pretty的清洁方式。

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.