登录验证全局控制的几种方式(session)
在登陆验证或者其他需要用到session全局变量的时候,归结起来,主要有以下三种较方便的实现方式。(其中个人较喜欢使用第一种实现方法)
一,在一个公共类里创建一个公共方法,然后需要验证的页面都调用这个方法
//在此例子中,就是在入口函数里调用CheckLogin()方法;[html]
public static string SeUserID
{
get
{
return HttpContext.Current.Session["SeUserID"].ToString();
}
set
{
HttpContext.Current.Session["SeUserID"] = value;
}
}
/// <summary>
/// 检查用户是否登录,如果未登录就转到登录页面
/// </summary>
public static void CheckLogin()
{
if (SeUserID == "" || SeUserID == "0")
{
HttpContext.Current.Response.Redirect("ForeignFirms.aspx");
//HttpContext.Current.Response.Write("<script>window.open('');alert('登陆失效,请重新登陆');</script>");
}
}
public static string SeUserID
{
get
{
return HttpContext.Current.Session["SeUserID"].ToString();
}
set
{
HttpContext.Current.Session["SeUserID"] = value;
}
}
/// <summary>
/// 检查用户是否登录,如果未登录就转到登录页面
/// </summary>
public static void CheckLogin()
{
if (SeUserID == "" || SeUserID == "0")
{
HttpContext.Current.Response.Redirect("ForeignFirms.aspx");
//HttpContext.Current.Response.Write("<script>window.open('');alert('登陆失效,请重新登陆');</script>");
}
}
[html]
//在cs页面调用验证方法
protected void Page_Load(object sender, EventArgs e)
{
Commom.CommonFunction.CheckLogin();//验证登陆信息
if (!IsPostBack)
{
GetData();
value = Request.QueryString["id"].ToString();
if (value != "0")
{
GetEdit();
}
}
}
//在cs页面调用验证方法
protected void Page_Load(object sender, EventArgs e)
{
Commom.CommonFunction.CheckLogin();//验证登陆信息
if (!IsPostBack)
{
GetData();
value = Request.QueryString["id"].ToString();
if (value != "0")
{
GetEdit();
}
}
}二,通过Global文件来控制
[html]
protected void Session_Start(Object sender, EventArgs e)
{
Session["sqlConnectionString"] = "uid=Username;pwd=password;database=MyTest;server=Localhost;Connect Timeout=300";
}
protected void Session_Start(Object sender, EventArgs e)
{
Session["sqlConnectionString"] = "uid=Username;pwd=password;database=MyTest;server=Localhost;Connect Timeout=300";
} --读取的方法,在代码中的应用[html] view plaincopyprint?
String strConnection=Session["sqlConnectionString"].ToString();
sqlConnection_1=new SqlConnection(strConnection);
String strConnection=Session["sqlConnectionString"].ToString();
sqlConnection_1=new SqlConnection(strConnection); 三,通过Web.Config文件配置
//配置Web.Config文件的方法如下:
在Web.Config文件的<system.web></system.web>节点中添加如下代码,设置Session的生命周期为10分钟。
[html]
<sessionState mode="InProc" timeout="10"></sessionState>
<sessionState mode="InProc" timeout="10"></sessionState> 在web.config文件中设置Session时,可以设置以下几个参数:[html] view plaincopyprint?
&nb
补充:web前端 , HTML/CSS ,