这边Session用来存储用户登录的唯一标识,用来判用户信息。
COOKIE用来记住密码,直接调用cookie就可以实现自动登录。
public static bool checkCookieInfo() {
HttpContext context = HttpContext.Current;
if (context.Request.Cookies["cp1"] != null && context.Request.Cookies["cp2"] != null) {
//都有值的话要进数据库进行查询
string un = context.Request.Cookies["cp1"].Value;
string pswd = context.Request.Cookies["cp2"].Value;
string siteId = context.Request.Cookies["siteId"].Value;
string isin = "select * from 用户基本信息表 where phoneNum = '"+un+"' and pswd ='"+pswd+"' and siteId='"+siteId+"'";
int i = dbUtil.QueryUserLogin(isin);
if (i == 1)
{
string sql = "select userId from 用户基本信息表 where phoneNum = '" + un + "' and pswd ='" + pswd + "' and siteId='" + siteId + "'";
string userId = dbUtil.GetDataSet(sql).Rows[0]["userId"].ToString();
context.Session["userId"] = userId;
return true;
}
}
return false;
}
用来判断用户cookie是否为空,如果不为空则进行数据库的查询,判断是否正确,如果不正确的话,就进入到登录页面。
if (Request.HttpMethod == "GET")
{
if (Session["userId"] != null)
{
Response.Redirect("/userIndex/userIndex.aspx"); //跳转到首页
}
else {
//进行自动登录
if (checkCookieInfo())
{
//这时候为true说明成功
Response.Redirect("/userIndex/userIndex.aspx"); //跳转到首页
}
else {
}
}
}
使用GET方法进行二次请求,判断如果唯一标识是空的话就进行自动登录调用,登录成功就进行跳转。