1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Security; 8 using System.Web.UI; 9 using System.Web.UI.HtmlControls; 10 using System.Web.UI.WebControls; 11 using System.Web.UI.WebControls.WebParts; 12 using System.Xml.Linq; 13 using System.Data.SqlClient; 14 using MS.DBUtility; 15 using WebValidates; 16 17 namespace myASUS 18 { 19 public partial class loadin : System.Web.UI.Page 20 { 21 string connStr = ConfigurationManager.ConnectionStrings["asusconnstr"].ConnectionString; 22 23 protected void Page_Load(object sender, EventArgs e) 24 { 25 //页面进行回发,IsPostBack为true 26 //页面第一次加载,IsPostBack为false 27 if (!Page.IsPostBack) 28 { 29 //如果页面第一次加载,读取Cookie里面存储的用户名,并显示 30 if (Request.Cookies["username"] != null) 31 { 32 userName.Text= Request.Cookies["username"].Value; 33 } 34 SerialNumber1.create(); 35 36 } 37 } 38 39 protected void btnLoadin_Click(object sender, EventArgs e) 40 { 41 42 if (SerialNumber1.CheckSN(txtCode.Text.Trim())) 43 { 44 //验证码正确 45 //登录验证 访问数据库 46 member user; 47 48 bool loginSuccess = LogIn(userName.Text,pwd.Text, out user); 49 if (loginSuccess) 50 { 51 //登录成功 52 53 //把当前用户的登录名写入客户端Cookie 54 //Request.Cookies["username"].Value = user.LoginId; 55 //Request.Cookies["username"].Expires = DateTime.Now.AddYears(1); 56 //用上面的方式来写入客户端Cookie会导致一个Bug的出现 57 //建议使用下面的方式: 58 59 //借助HttpCookie对象,写入客户端Cookie 60 //HttpCookie cookie = new HttpCookie("username", user.LoginId); 61 HttpCookie cookie = new HttpCookie("username"); 62 cookie.Value = user.membername; 63 cookie.Expires = DateTime.Now.AddYears(1); 64 Response.Cookies.Add(cookie); 65 66 67 //Session[Session名称]=值; 68 Session["CurrentUser"] = user; //CurrentUser: 当前用户 69 70 //获取HTTP查询字符串(QueryString) ?ReturnUrl=MyOrders.aspx 71 72 if (Request.QueryString["ReturnUrl"] != null) 73 { 74 string url = Request.QueryString["ReturnUrl"]; 75 Response.Redirect(url); 76 } 77 else 78 { 79 Response.Redirect("Default.aspx"); 80 } 81 } 82 else 83 { 84 //登录失败 85 Response.Write("<script>"); 86 Response.Write(" alert('用户名或密码错误!')"); 87 Response.Write("</script>"); 88 } 89 } 90 else 91 { 92 //验证码不正确 93 Response.Write("<script>"); 94 Response.Write(" alert('验证码错误!')"); 95 Response.Write("</script>"); 96 97 SerialNumber1.create(); 98 99 } 100 101 } 102 public bool LogIn(string memberName, string Password, out member validUser) 103 { 104 member user = GetUserByLoginId(memberName); 105 if (user == null) 106 { 107 validUser = null;//用户名不存在 108 return false; 109 } 110 111 if (user.password==Password) 112 { 113 validUser = user; 114 return true; 115 } 116 else 117 { 118 validUser = null;//密码错误 119 return false; 120 } 121 } 122 public member GetUserByLoginId(string memberName) 123 { 124 member user = null; 125 string sql = "SELECT * FROM ASUS WHERE memberName= @memberName"; 126 127 using (SqlDataReader reader = SqlHelper.ExecuteReader(connStr, CommandType.Text, sql, new SqlParameter("@memberName",memberName))) 128 { 129 if (reader.Read()) 130 { 131 user = new member(); 132 133 user.memberId= (int)reader["memberID"]; 134 user.membername= (string)reader["memberName"]; 135 user.password= (string)reader["Password"]; 136 reader.Close(); 137 } 138 } 139 return user; 140 } 141 public bool CheckCN() 142 { 143 string validateCode = Session["validateCode"].ToString(); 144 if (this.txtCode.Text != validateCode) 145 { 146 Response.Write("<script>alert('验证码输入错误,请重新输入!');</script>"); 147 txtCode.Text = ""; 148 return false; 149 } 150 else 151 { 152 return true; 153 } 154 } 155 protected void btnClear_Click(object sender, EventArgs e) 156 { 157 userName.Text = ""; 158 pwd.Text = ""; 159 } 160 } 161 } 162 下面是页面代码 163 <body> 164 <form id="form1" runat="server"> 165 <div id="login"> 166 167 <div id="top"> 168 <div id="top_left"><img src="image/login_03.gif" alt="#"/></div> 169 <div id="top_center"></div> 170 </div> 171 172 <div id="center"> 173 <div id="center_left"></div> 174 <div id="center_middle"> 175 <div id="user">用 户 176 <asp:TextBox ID="userName" runat="server"></asp:TextBox> 177 </div> 178 <div id="password">密 码 179 <asp:TextBox ID="pwd" runat="server"></asp:TextBox> 180 </div> 181 <div> 182 <asp:Label ID="lblCode" runat="server" Text="验证码"></asp:Label> 183 <asp:TextBox ID="txtCode" runat="server"></asp:TextBox> 184 </div> 185 <div> 186 <cc2:SerialNumber ID="SerialNumber1" runat="server"> 187 </cc2:SerialNumber> 188 </div> 189 <div id="btn"> 190 <asp:Button ID="btnLoadin" runat="server" Text="登录" onclick="btnLoadin_Click" /> 191 <asp:Button ID="btnClear" runat="server" Text="清空" onclick="btnClear_Click" /> 192 </div> 193 194 </div> 195 <div id="center_right"></div> 196 </div> 197 <div id="down"> 198 <div id="down_left"> 199 <div id="inf"> 200 <span class="inf_text">版本信息</span> 201 <span class="copyright">管理信息系统</span> 202 </div> 203 </div> 204 <div id="down_center"></div> 205 </div> 206 207 </div> 208 </form> 209 </body>
添加了两个验证码,我使用的是第二个
等待您拯救!!!
http://blog.csdn.net/xiangxiao_bobo/article/details/6221937
不行啊,我就是按照这些步奏加的
添加引用,将类库加进去就行
咋么加