示例:用户登录、注册
-》使用Session进行验证码判断
-》使用Session完成登录验证
-》使用Cookie记住用户名
Register.aspx
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="t5_UserLogin_Register.Register" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 8 <title></title> 9 </head> 10 <body> 11 <form id="form1" runat="server"> 12 用户名:<input type="text" name="uname" /> 13 <br /> 14 密码:<input type="password" name="upwd" /> 15 <br /> 16 验证码:<input type="text" name="vcode" /> 17 <img src="ValidateCode.ashx" /> 18 <br /> 19 <input type="submit" value="注册" /> 20 <br /> 21 <%=Msg %> 22 </form> 23 </body> 24 </html>
1 public partial class Register : System.Web.UI.Page 2 { 3 protected string Msg { get; set; } 4 protected void Page_Load(object sender, EventArgs e) 5 { 6 if (IsPostBack) 7 { 8 //回传处理 9 string uname = Request["uname"]; 10 string upwd = Request["upwd"]; 11 string vcode = Request["vcode"]; 12 13 object vcode2 = Session["vCode"]; 14 if (vcode2 == null) 15 { 16 return; 17 } 18 if (vcode2.ToString().Equals(vcode)) 19 { 20 //验证码正确 21 22 //执行注册操作 23 string sql = "insert into userinfo values(@name,@pwd)"; 24 SqlParameter[] ps = 25 { 26 new SqlParameter("@name", uname), 27 new SqlParameter("@pwd", upwd) 28 }; 29 30 if (SqlHelper.ExecuteNonQuery(sql, ps) > 0) 31 { 32 //转向登录页面 33 Response.Redirect("Login.aspx"); 34 } 35 else 36 { 37 Session["vCode"] = null; 38 Msg = "注册失败"; 39 } 40 } 41 else 42 { 43 //强制清除:防止暴力破解 44 Session["vCode"] = null; 45 Msg = "验证码错误"; 46 } 47 } 48 } 49 }
Login.aspx
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="t5_UserLogin_Register.Login" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 8 <title></title> 9 </head> 10 <body> 11 <form id="form1" runat="server"> 12 用户名:<input type="text" name="uname" value="<%=UserName %>" /> 13 <br /> 14 密码: 15 <input type="password" name="upwd" /> 16 <br /> 17 <input type="checkbox" name="rememberMe" value="1" checked="checked" />记住我 18 <br /> 19 <input type="submit" value="登录" /> 20 </form> 21 </body> 22 </html>
1 public partial class Login : System.Web.UI.Page 2 { 3 protected string UserName { get; set; } 4 protected string Msg { get; set; } 5 protected void Page_Load(object sender, EventArgs e) 6 { 7 HttpCookie cookie1 = Request.Cookies["username"]; 8 if (cookie1 != null) 9 { 10 UserName = cookie1.Value; 11 } 12 13 if (IsPostBack) 14 { 15 //点击登录按钮后,执行如下代码 16 17 string uname = Request["uname"]; 18 string upwd = Request["upwd"]; 19 20 string sql = "select userPwd from userinfo where userName=@name"; 21 SqlParameter p = new SqlParameter("@name", uname); 22 23 object pwd = SqlHelper.ExecuteScalar(sql, p); 24 if (pwd == null) 25 { 26 Msg = "用户名不存在"; 27 } 28 else if (pwd.ToString().Equals(upwd)) 29 { 30 //密码正确,登录成功 31 32 //记住我 33 if (string.IsNullOrEmpty(Request["rememberMe"])) 34 { 35 //没有勾选 36 cookie1.Expires = DateTime.Now.AddMinutes(-1); 37 Response.Cookies.Add(cookie1); 38 } 39 else 40 { 41 //写cookie进行记录 42 HttpCookie cookie = new HttpCookie("username", uname); 43 cookie.Expires = DateTime.Now.AddDays(14); 44 Response.Cookies.Add(cookie); 45 } 46 47 Session["login"] = "xlb"; 48 Response.Redirect("index.aspx"); 49 } 50 else 51 { 52 Msg = "密码错误"; 53 } 54 } 55 } 56 }
Index.aspx
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="t5_UserLogin_Register.Index" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 8 <title></title> 9 </head> 10 <body> 11 <form id="form1" runat="server"> 12 <div> 13 <h1>小龙包,终于来了</h1> 14 </div> 15 </form> 16 </body> 17 </html>
1 public partial class Index : System.Web.UI.Page 2 { 3 protected void Page_Load(object sender, EventArgs e) 4 { 5 if (Session["login"] == null) 6 { 7 //未登录,转向登录页面 8 Response.Redirect("Login.aspx"); 9 } 10 } 11 }
SqlHelper.cs
1 public static class SqlHelper 2 { 3 private static string connStr = 4 System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; 5 6 public static int ExecuteNonQuery(string sql, params SqlParameter[] ps) 7 { 8 using (SqlConnection conn = new SqlConnection(connStr)) 9 { 10 SqlCommand cmd = new SqlCommand(sql, conn); 11 cmd.Parameters.AddRange(ps); 12 13 conn.Open(); 14 return cmd.ExecuteNonQuery(); 15 } 16 } 17 18 public static object ExecuteScalar(string sql, params SqlParameter[] ps) 19 { 20 using (SqlConnection conn = new SqlConnection(connStr)) 21 { 22 SqlCommand cmd = new SqlCommand(sql, conn); 23 cmd.Parameters.AddRange(ps); 24 25 conn.Open(); 26 return cmd.ExecuteScalar(); 27 } 28 } 29 }
ValidateCode.ashx
1 public class ValidateCode : IHttpHandler, IRequiresSessionState 2 { 3 4 public void ProcessRequest(HttpContext context) 5 { 6 context.Response.ContentType = "image/jpeg"; 7 8 //创建画布 9 Bitmap bitmap = new Bitmap(70, 30); 10 11 //bitmap.SetPixel(x,y,color); 12 13 //获取绘制工具 14 Graphics graphics = Graphics.FromImage(bitmap); 15 16 //刷背景 17 graphics.Clear(Color.White); 18 19 //绘制边框 20 graphics.DrawRectangle(new Pen(Color.Black), 0, 0, 69, 29); 21 22 //获取字符串 23 string temp = "abcdef0123456789"; 24 25 int len = 4, len2 = temp.Length; 26 StringBuilder sb = new StringBuilder(""); 27 Random random = new Random(); 28 for (int i = 0; i < len; i++) 29 { 30 int index = random.Next(0, len2); 31 sb.Append(temp[index].ToString()); 32 } 33 string temp1 = sb.ToString(); 34 //进行状态保持 35 context.Session["vCode"] = temp1; 36 37 //绘制字符串 38 graphics.DrawString(temp1, 39 new Font("宋体", 20), 40 new SolidBrush(Color.Red), 41 0, 0 42 ); 43 44 //保存 45 bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg); 46 47 } 48 49 public bool IsReusable 50 { 51 get 52 { 53 return false; 54 } 55 } 56 }