Global.asax.cs 中的 Session_End 未使用表单身份验证触发
我有一个使用表单身份验证的 asp.net 4.0 应用程序,超时设置为 45 分钟。我想在会话过期时将用户重定向到超时页面。谁能告诉我该怎么做?我正在运行.net 4.0。
web.config 有:
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="~/Login.aspx"
defaultUrl="~/Default.aspx" protection="All" timeout="45"
requireSSL="false">
</forms>
</authentication>
Global.asax.cs 文件有:
void Session_End(object sender, EventArgs e)
{
Response.Redirect("~/Timeout.aspx");
}
I have an asp.net 4.0 application that is using forms authentication set to a timeout at 45 minutes. I would like to redirect the user to a timeout page when the session has expired. Can anyone tell me how to do this? I am running .net 4.0.
web.config has:
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="~/Login.aspx"
defaultUrl="~/Default.aspx" protection="All" timeout="45"
requireSSL="false">
</forms>
</authentication>
Global.asax.cs file has:
void Session_End(object sender, EventArgs e)
{
Response.Redirect("~/Timeout.aspx");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无法在
Session_End
方法中进行重定向。它不是作为请求的结果而运行,因此它没有Response
对象,并且没有可重定向到任何地方的响应。由于会话过期,无法在浏览器中执行任何操作。 HTTP 协议是面向请求的,因此如果浏览器没有请求,就无法将消息从服务器推送到浏览器。
浏览器只是无法确定会话是否已过期。如果您轮询服务器以检查会话是否已过期,它将使会话保持活动状态,从而违背了超时的目的。
您可以仅使用客户端脚本在 45 分钟后进行重定向:
但是,这将仅根据此浏览器窗口上次联系服务器以来的时间进行重定向。如果同一会话有多个浏览器窗口,则该会话可能实际上并未超时。
It's not possible to do a redirect in the
Session_End
method. It's not running as a result of a request, so it doesn't have aResponse
object and there is no response to redirect anywhere.It's not possible to do anything in the browser as a result of the session expiring. The HTTP protocol is request oriented, so there is no way to push a message from the server to the browser without the browser asking for it.
The browser just can't find out if the session has expired or not. If you would poll the server to check if the session has expired, it would keep the session alive, defeating the purpose of the timeout.
You can make a redirect after 45 minutes using just client script:
However, this will make the redirect only based on the time since this browser window last contacted the server. If you have more than one browser window for the same session, it's possible that the session has actually not timed out.
你的会话状态是如何实现的? Session_End 仅在您使用 InProc 时才有效。
请参阅http://www.eggheadcafe.com/articles/20021016.asp
How is your session state implemented? Session_End only works when you are using InProc.
See http://www.eggheadcafe.com/articles/20021016.asp
在 MVC 上,您可以在 _ViewStart.cshtml _ViewStart.cshtml 中添加以下代码
:
如何在会话结束时重定向
On MVC you can adding this code in _ViewStart.cshtml
_ViewStart.cshtml:
How to Redirect on Session End