验证码登录
<script language="javascript" type="text/javascript"> function change() { document.getElementById("imgCode").src="Code.aspx?"+Math.random(); }
<tr> <td align="center" valign="middle" class="Zitilansedenglu"> 验证:</td> <td align="left" valign="middle" class="Zitilansedenglu"> <%-- <input name="admincode" type="text" class="Denglubiaodaner" id="admincode" />--%> <input name="TxtCode" type="text" class="Denglubiaodaner" id="TxtCode" /> <b id="Code"> <img src="Code.aspx" id="imgCode" onclick="change()" onmouseover="this.style.cursor='hand'" height="25px" /></b> </td> </tr>
using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Data.OleDb; using webA; using webinfo; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void BtnSubmit_Click(object sender, ImageClickEventArgs e) { if (Page.IsValid == true) { Admins admin = Login2.ValidateUser(TextBox1.Text.Trim(), TextBox2.Text.Trim()); if (admin == null || admin.ID == 0) { Response.Write("<script type='text/javascript'>alert('您输入的用户名和密码有误,请重新输入!!');</script>"); } else { Session["user"] = admin; Response.Redirect("Main.aspx"); } } } }
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Data.OleDb; using webA; namespace webinfo { /// <summary> ///Login 的摘要说明 /// </summary> public static class Login2 { public static Admins ValidateUser(string adminname, string adminpassword) { string sqlCommand = "select adminname,adminpassword ,ID from web_admin where " + "(adminname = ?) and (adminpassword = ?)"; OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath("~/App_Data/www.web.com.mdb")); OleDbCommand command = new OleDbCommand(); command.Parameters.Add(new OleDbParameter("adminname", adminname)); command.Parameters.Add(new OleDbParameter("adminpassword", Common.GetMD5(adminpassword))); command.Connection = conn; command.CommandText = sqlCommand; conn.Open(); Admins admin = null; using (OleDbDataReader dataReader = command.ExecuteReader()) { admin = new Admins(); if (dataReader.Read()) { admin.ID = int.Parse(dataReader["ID"].ToString()); admin.adminname = dataReader["adminname"].ToString(); } } conn.Close(); return admin; } } }
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.Drawing; public partial class Code : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { string strCode = this.RndNum(4); Session["CodeValue"] = strCode; this.CreateImage(strCode); } } private void CreateImage(string checkCode) { int iwidth = (int)(checkCode.Length * 15); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 25); Graphics g = Graphics.FromImage(image); g.Clear(Color.White); ////定义颜色 //Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.DarkCyan, Color.Purple, Color.YellowGreen }; ////定义字体 //string[] font = { "Times New Roman", "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; //定义颜色 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.DarkCyan, Color.Purple, Color.YellowGreen }; //定义字体 string[] font = { "Times New Roman","Microsoft Sans Serif", "Comic Sans MS" }; Random rand = new Random(); //随机输出噪点 for (int i = 0; i < 50; i++) { int x = rand.Next(image.Width); int y = rand.Next(image.Height); g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1); } //输出不同字体和颜色的验证码字符 for (int i = 0; i < checkCode.Length; i++) { int cindex = rand.Next(7);//从7种颜色中随机取 int findex = rand.Next(3);//从6种字体中随机取 Font f = new System.Drawing.Font(font[findex], 13, System.Drawing.FontStyle.Regular); Brush b = new System.Drawing.SolidBrush(c[cindex]); int ii = 4; if ((i + 1) % 2 == 0) { ii = 2; } g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 12), ii); } //画一个边框 g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1); //输出到浏览器 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ClearContent(); Response.ContentType = "image/Jpeg"; Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); } //生成随机数函数中从Vchar数组中随机抽取 //字母区分大小写 private string RndNum(int VcodeNum) { string Vchar = "0,1,2,3,4,5,6,7,8,9"; //Vchar = Vchar + ",A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; string[] VcArray = Vchar.Split(','); string VNum = "";//由于字符串很短,就不用StringBuilder了 int temp = -1;//记录上次随机数值,尽量避免生产几个一样的随机数 //采用一个简单的算法以保证生成随机数的不同 Random rand = new Random(); for (int i = 1; i < VcodeNum + 1; i++) { if (temp != -1) { rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks)); } int t = rand.Next(10); if (temp != -1 && temp == t) { return RndNum(VcodeNum); } temp = t; VNum += VcArray[t]; } return VNum; } }