Session:
登录页面
1 Session["UserName"] = sUsername;
其他页面
1 if (Session["UserName"] == null) 2 { 3 Response.Redirect("Index.aspx"); 4 }
说明:看懂用法即可
Cookie:
登录页面:
1 if(CheckBoxRemeber.Checked) 2 { 3 Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(7); 4 //cookieHelper.SetCookie("UserName", sUsername, 60*24*7);//using com.Utility; 5 Response.Cookies["Pwd"].Expires = DateTime.Now.AddDays(7); 6 //cookieHelper.SetCookie("Pwd", sPwd, 60*24*7);//using com.Utility; 7 } 8 Response.Cookies["UserName"].Value = sUsername; 9 Response.Cookies["Pwd"].Value = sPwd; 10 Response.Redirect("UserM.aspx");
其他页面:
1 if (Request.Cookies["UserName"] == null) 2 { 3 Response.Redirect("Index.aspx"); 4 } 5 if (cookieHelper.GetCookie("UserName") == null) 6 { 7 Response.Redirect("Index.aspx"); 8 }
效果: 如果没有输入用户名和密码的话就会滚回到登录页面.
原文:http://www.cnblogs.com/anwser-jungle/p/6435904.html