大佬教程收集整理的这篇文章主要介绍了ASP.NET 做验证码,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
显示验证码的界面: 前台: <td class="style4"> 验证码:</td> <td class="style2"> <asp:TextBox ID="TextBox3" runat="server" Height="21px"></asp:TextBox> <asp:Image ID="Image1" runat="server" ImageUrl="~/Default2.aspx"/> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">看不清换张图</asp:HyperLink> </td> 后台: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Configuration; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender,EventArgs E) { } protected void Button1_Click(object sender,EventArgs E) { String str = ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString; using (SqlConnection sqlCnn=new SqlConnection(str)) { using (SqlCommand sqlCmm=sqlCnn.CreateCommand()) { sqlCmm.CommandText = "SELEct * from Login"; sqlCnn.open(); SqlDataReader reader = sqlCmm.ExecuteReader(); bool bl = false; if (reader!=null) { while (reader.Read()) { if (this.TextBox1.Text == reader["name"].ToString() && this.TextBox2.Text == reader["password"].ToString() && this.TextBox3.Text == Session["code"].ToString()) { bl = true; } } if (bl==truE) { ClientScript.RegisterClientScriptBlock(GetType(),"提示","<script>alert('登陆成功!')</script>"); } else { ClientScript.RegisterClientScriptBlock(GetType(),"<script>alert('登陆失败!')</script>"); } } } } } } 做验证码的界面: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using System.IO; using System.Text; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender,EventArgs E) { System.Drawing.Image img = new Bitmap(100,20); Graphics g = Graphics.FromImage(img); this.DealImage(img,10); g.DrawLine(Pens.Yellow,100,20); String code = this.GenerateCode(); Font font = new Font("宋体",20,FontStyle.bold | FontStyle.Italic | FontStyle.Strikeout | FontStyle.UnderlinE); Session["code"] = code; //保存到Session中 g.DrawString(code,font,Brushes.YellowGreen,0); this.Response.Clear();//http://www.baoluowanxiang.com MemoryStream ms = new MemoryStream(); img.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); this.Response.binaryWrite(ms.ToArray()); this.Response.Flush(); this.Response.End(); } private void DealImage(System.Drawing.Image img,int nums) { Bitmap b = img as Bitmap; Random ran = new Random(); for (int i = 0; i < nums; i++) { b.SetPixel(ran.Next(0,img.Width),ran.Next(0,img.Height),Color.WhitE); } } private String GenerateCode() //产生随机数 { Random ran = new Random(datetiR_665_11845@e.Now.Millisecond); StringBuilder sb = new StringBuilder(6); for (int i = 0; i <6; i ++) { sb.Append(ran.Next(0,9)); } return sb.ToString(); } }
以上是大佬教程为你收集整理的ASP.NET 做验证码全部内容,希望文章能够帮你解决ASP.NET 做验证码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。