简体   繁体   中英

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

I am working on an asp.net C# webforms project. I want to redirect to login.aspx page when the session expires. I have the javascript based solutions available on the internet which work fine. However I would like it to work off of global.asax page so that the code will be in one place. 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. 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. 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. In other words, when a session expires, Session_End will fire. 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:

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/

So, you may not want to rely on Session_End in the first place.

Since you don't have a Request, you can't do a redirect from the server. You have to do it in JavaScript. But if you really need the server to "push" the redirect to any connected clients, you could use WebSockets to accomplish this. (I think that would be a bad idea, though.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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