以下是判断Session是否有效的方法
public static void CheckSession()
        {
            try
            {
                string CheckUser = System.Web.HttpContext.Current.Session["UserName"].ToString();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<script>alert('登陆信息安全时限过期,请重新登陆!');top.location='Login.aspx'</script>");
            }
        } 下面是页面加载的方法:
1    protected void Page_Load(object sender, EventArgs e)
2        {
3           if (!IsPostBack)
4           {
5                PublicMethodManager.CheckSession();
6                if (PublicMethodManager.StrIFIn("|004|", Session["PerStr"].ToString()) == false)
7                {
8                    Page.Server.Transfer("NoAuthored.aspx");
9
10                }
11                else
12                {
13                    BindAttribute();
14                    BindCooperate();
15                }
16            }
17        }
其中,第6行是判断用户权限的。
现在的情况是,当Session过期后,用户点击该页面,在第6行就报错:未将对象引用设置到对象的实例。 
我的困惑是如果Session过期,那么走到第5行就应该跳到Login.aspx页面,不往下走。
为什么会是这样,该怎么解决呢?高手们

解决方案 »

  1.   

    PublicMethodManager.StrIFIn("|004|", Session["PerStr"].ToString())这个函数的实现贴一下
      

  2.   


    这行就是判断该页面的"|004|"是否在权限用户的权限字符串中"PerStr",Session["PerStr"]在登陆页面定义,即该用户的权限字符串。具体实现如下:
    public static bool StrIFIn(string Str1, string Str2)
            {
                if (Str2.IndexOf(Str1) < 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
      

  3.   

    public static bool StrIFIn(string Str1, string Str2) 
    这个函数的第二个参数,Str2是用Session的值,
    session超时了, Session["PerStr"].ToString()肯定会报错。
      

  4.   

    Session["PerStr"].ToString()) == null
      

  5.   

    我知道肯定会报错,但是session超时后,我觉得走到第5行就不应该往下走了,为什么呢?
      

  6.   

    第五行你只是做了异常处理,当然还会往下执行,除非return
      

  7.   

    public static void CheckSession()
            {
                try
                {
                    string CheckUser = System.Web.HttpContext.Current.Session["UserName"].ToString();
                }
                catch
                {
                    System.Web.HttpContext.Current.Response.Write(" <script>alert('登陆信息安全时限过期,请重新登陆!');top.location='Login.aspx' </script>");//-------------- 添加一句:Session过期,就结束执行代码。
    System.Web.HttpContext.Current.Response.End();            }
            } 
      

  8.   

    解决了,修改为一下的:public static bool CheckSession()
            {
                try
                {
                    string CheckUser = System.Web.HttpContext.Current.Session["UserName"].ToString();
                    return true;
                }
                catch
                {
                    System.Web.HttpContext.Current.Response.Write("<script>alert('登陆信息安全时限过期,请重新登陆!');top.location='Login.aspx'</script>");
                    return false;
                }
            }