如何重定向会话结束 [英] How to Redirect on Session End

查看:160
本文介绍了如何重定向会话结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在会话结束后用户重定向到另一个页面。

I am trying to redirect user to another page when session ends.

这code会导致未将对象引用设置到对象的实例。例外

This code will result "Object reference not set to an instance of an object." exception

void Session_End(Object sender, EventArgs E)
{
    HttpContext.Current.Response.Redirect("/");
}

任何想法,该怎么办呢?

any idea how to do it ?

推荐答案

Session_End中由服务器内部解雇。正因为如此,没有的Htt prequest 当这种情况发生有关。这就是为什么的Response.Redirect Server.Transfer的无厘头,将无法正常工作。

Session_End is fired internally by the server, based on an internal timer. Because of that, there is no HttpRequest associated when that happens. That is why Response.Redirect or Server.Transfer does not make sense and will not work.

我看到在这个过去的解决方法,但从来没有尝试过,你应该让基类,它的每一页继承。 的OnInit 在基类中添加此。基类继承 UI.Page 如果你不使用的基类逻辑,你应该将这个逻辑添加到每一页这是不是很好。

I see in the past workarounds about this but never tried, you should make Base class which every page inherit. OnInit in the base class add this. Base Class inherit UI.Page. If you don't use base class logic you should add this logic to every page which is not nice.

    protected override void OnInit(System.EventArgs e)
    {
         Response.AddHeader("Refresh",Convert.ToString((Session.Timeout * 60) + 5));      

         if(Session.IsNewSession)  
            Response.Redirect("Logout.aspx");// or another page which you want.
    }

该页面应该在5秒钟后一旦会话过期被刷新,与如果你赶上,这次会议是新的,你会重定向。

The page should be refreshed after 5 seconds once the Session is expired, with the if you will catch that the session is new and you will redirect.

这篇关于如何重定向会话结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆