简体   繁体   English

会话结束时如何从Global.asax重定向到login.aspx页面

[英]How to redirect to login.aspx page off of Global.asax when session ends

I am working on an asp.net C# webforms project. 我正在一个asp.net C#Webforms项目上。 I want to redirect to login.aspx page when the session expires. 我想在会话过期时重定向到login.aspx页面。 I have the javascript based solutions available on the internet which work fine. 我在互联网上可以使用基于javascript的解决方案,效果很好。 However I would like it to work off of global.asax page so that the code will be in one place. 但是,我希望它在global.asax页面上工作,以便将代码放在一个位置。 I tried to redirect from the Session_End event on the Global.asax but didn't work as no Response and Request object available to this event. 我尝试从Global.asax上的Session_End事件重定向,但是由于该事件没有可用的Response和Request对象,因此无法正常工作。 There were some references to use Application_AcquireRequestState event as in Redirecting to another page on Session_end event but it didn't work either. 重定向到Session_end事件的另一个页面中时,有一些使用Application_AcquireRequestState事件的引用,但是它也不起作用。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thanks 谢谢

It's been a while since I've lived in the .NET world, but as far as I recall, Session_End fires on the server without any request triggering it. 自从我生活在.NET世界以来已经有一段时间了,但是据我所记得, Session_End在服务器上触发而没有任何触发它的请求。 In other words, when a session expires, Session_End will fire. 换句话说,当会话到期时, Session_End将触发。 You won't have a Request at that point in time. 那时您将没有请求。

Also, if you ever need to move to external sessions, Session_End won't fire: 另外,如果您需要进行外部会话,则Session_End将不会触发:

https://blogs.msdn.microsoft.com/amb/2011/06/03/session_onend-or-session_end-events-in-global-asax-wont-fire-if-you-store-asp-net-sessions-out-of-proc-in-state-server-or-sql-server/ https://blogs.msdn.microsoft.com/amb/2011/06/03/session_onend-or-session_end-events-in-global-asax-wont-fire-if-you-store-asp-net-sessions-在状态服务器或SQL Server中退出进程/

So, you may not want to rely on Session_End in the first place. 因此,您可能首先不希望依赖Session_End

Since you don't have a Request, you can't do a redirect from the server. 由于没有请求,因此无法从服务器进行重定向。 You have to do it in JavaScript. 必须使用JavaScript来完成。 But if you really need the server to "push" the redirect to any connected clients, you could use WebSockets to accomplish this. 但是,如果您确实需要服务器将“重定向”到任何连接的客户端,则可以使用WebSockets完成此操作。 (I think that would be a bad idea, though.) (不过,我认为这是个坏主意。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM