为了使验证用户是否已经登陆的部分少写代码,我在当前项目中新建了一个类,并在每个webfrom 的page_load 里加了 Safety.IsLogging(),下面是类的内容:using System;
namespace HTWatchSys
{
/// <summary>
/// Safety 的摘要说明。
/// </summary>
public  class Safety
{
/// <summary>
/// 验证系统是否登陆
/// </summary>
public static void IsLogging()
{
if (System.Web.HttpContext.Current.Session["U_NAME"]==null)
{
string script = "<script language=\"javascript\">\r<!--\ralert(\"会话已结束,请重新登录。\");opener=null;window.location.href=\"login.aspx\";\r// -->\r</script>";
System.Web.HttpContext.Current.Response.Write(script);
System.Web.HttpContext.Current.Response.End();
}
} /// <summary>
/// 保存登陆状态
/// </summary>
/// <param name="obj"></param>
public static void SaveLogging(string obj)
{
System.Web.HttpContext.Current.Session["U_NAME"]=obj;
} }
}问题1、
登陆的时执行了 SaveLogging(),并跟踪当前页发现session有值,但是进入另一个页面的时候在执行IsLogging 时却没有值,请问这是怎么回事啊?问题2、
因为上面的问题,我怀疑不能把验证部分写到一个类中方便其他窗体调用,请问一般网站中用 session 来验证用户是否登陆,都是分别在每个webfrom里写的验证吗?如果有其他做法希望能提供点思路,谢谢各位了!