asp.net基业控制登录简单实现
先看下cs页面的完整代码
方法很简单:重写了Page类的OnLoad事件。加入判断,看Cookies是否存在。
若不存在则跳转到Login.aspx页面,提示登录。
若存在判断数据库中是否有Cookies中保存的用户信息是否与数据库中的信息符合,否则也跳转到登录页面。
其他需要登录才可访问的页面只需继承于此页面即可。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class mainpage : System.Web.UI.Page
{
SqlConnection sqlCon;
SqlCommand sqlCom;
protected override void OnLoad(EventArgs e)
{
if (Request.Browser.Cookies == true)
{
if (Request.Cookies["NewShopCK"] == null)
{
Session["PreviousPage"] = this.Request.Path;
Response.Redirect("login.aspx");
}
else
{
yemian();
}
}
else
{
Response.Redirect("nocookies.aspx");
}
base.OnLoad(e);
}
protected void yemian()
{
string shopuser = Convert.ToString(Request.Cookies["NewShopCK"].Values["shopuser"]);
string strCon = ConfigurationManager.ConnectionStrings["newshop"].ConnectionString;
string strCom = "select * from UserList where ULName='" + shopuser + "'";
sqlCon = new SqlConnection(strCon);
sqlCom = new SqlCommand(strCom, sqlCon);
SqlDataReader sqlReader;
sqlCom.Connection.Open();
sqlReader = sqlCom.ExecuteReader(CommandBehavior.CloseConnection);
if (!sqlReader.HasRows)
{
Session["PreviousPage"] = this.Request.Path;
Response.Redirect("login.aspx");
sqlCom.Dispose();
sqlCon.Close();
}
sqlCom.Dispose();
sqlCon.Close();
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class mainpage : System.Web.UI.Page
{
SqlConnection sqlCon;
SqlCommand sqlCom;
protected override void OnLoad(EventArgs e)
{
if (Request.Browser.Cookies == true)
{
if (Request.Cookies["NewShopCK"] == null)
{
Session["PreviousPage"] = this.Request.Path;
Response.Redirect("login.aspx");
}
else
{
yemian();
}
}
else
{
Response.Redirect("nocookies.aspx");
}
base.OnLoad(e);
}
protected void yemian()
{
string shopuser = Convert.ToString(Request.Cookies["NewShopCK"].Values["shopuser"]);
string strCon = ConfigurationManager.ConnectionStrings["newshop"].ConnectionString;
string strCom = "select * from UserList where ULName='" + shopuser + "'";
sqlCon = new SqlConnection(strCon);
sqlCom = new SqlCommand(strCom, sqlCon);
SqlDataReader sqlReader;
sqlCom.Connection.Open();
sqlReader = sqlCom.ExecuteReader(CommandBehavior.CloseConnection);
if (!sqlReader.HasRows)
{
Session["PreviousPage"] = this.Request.Path;
Response.Redirect("login.aspx");
sqlCom.Dispose();
sqlCon.Close();
}
sqlCom.Dispose();
sqlCon.Close();
}
}
方法很简单:重写了Page类的OnLoad事件。加入判断,看Cookies是否存在。
若不存在则跳转到Login.aspx页面,提示登录。
若存在判断数据库中是否有Cookies中保存的用户信息是否与数据库中的信息符合,否则也跳转到登录页面。
其他需要登录才可访问的页面只需继承于此页面即可。
public partial class MemberPage : mainpage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
}
}