以下是判断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页面,不往下走。
为什么会是这样,该怎么解决呢?高手们
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页面,不往下走。
为什么会是这样,该怎么解决呢?高手们
这行就是判断该页面的"|004|"是否在权限用户的权限字符串中"PerStr",Session["PerStr"]在登陆页面定义,即该用户的权限字符串。具体实现如下:
public static bool StrIFIn(string Str1, string Str2)
{
if (Str2.IndexOf(Str1) < 0)
{
return false;
}
else
{
return true;
}
}
这个函数的第二个参数,Str2是用Session的值,
session超时了, Session["PerStr"].ToString()肯定会报错。
{
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(); }
}
{
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;
}
}