2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > ASP.NET MVC ---MVC中验证码

ASP.NET MVC ---MVC中验证码

时间:2022-07-31 12:41:58

相关推荐

ASP.NET MVC ---MVC中验证码

验证码的作用是为了防止物理盗号,或者暴力破坏服务器

图片验证码生成

模型的实现

首先在Models文件夹添加实现验证码的模型CreateValidateCode

public class CreateValidateCode{private string CreateRandomCode(int codeCount){string code = "";Random r = new Random();//偶数给数字,奇数给小写字母for (int i = 0; i < codeCount; i++){//小写字母和数字int type = r.Next(100);if (type % 2 == 0){code += r.Next(0, 10);}else{code += (char)r.Next(97, 123);}}return code;}/// <summary>/// 生成验证码/// </summary>/// <param name="codeCount"></param>/// <returns></returns>public byte[] CreateValidateGraphic(int codeCount,out string content){string code = CreateRandomCode(codeCount);content = code;//验证码图片初始化Bitmap img = new Bitmap((int)Math.Ceiling(code.Length * 16.0), 27);Graphics g = Graphics.FromImage(img);try{Random r = new Random();g.Clear(Color.White);//清空背景//干扰线30条for (int i = 0; i < 30; i++){int x1 = r.Next(img.Width);int y1 = r.Next(img.Height);int x2 = r.Next(img.Width);int y2 = r.Next(img.Height);g.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);}Font font = new Font("Arial",13, (FontStyle.Bold| FontStyle.Italic));LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.DarkRed, 0.25f, true);//设置画刷g.DrawString(code, font, brush, 3, 3);//干扰点100个for (int i = 0; i < 100; i++){int x = r.Next(img.Width);int y = r.Next(img.Height);img.SetPixel(x,y,Color.FromArgb(r.Next()));}​g.DrawRectangle(new Pen(Color.Black),0,0,img.Width-1,img.Height-1);//保存图片MemoryStream stream = new MemoryStream();img.Save(stream, ImageFormat.Jpeg);return stream.ToArray();}finally{g.Dispose();img.Dispose();}}}

控制器的实现

//生成验证码图片public ActionResult ValidateCode(){//1.生成一个随机的4位验证码CreateValidateCode create = new CreateValidateCode();string code = "";//2.生成验证码并生成图片文件的字节流byte[] arry = create.CreateValidateGraphic(4,out code);//3.将生成的验证码保存在TempData里面,验证码在开发里面都是通过Session保存的,所以用Session也行,用TempData也行TempData["VaildataCode"] = code;//4.返回验证码图片return File(arry,"image/Jpeg");}

视图的实现

<tr><td>验证码:</td><td><input type="text" name="ValidateCode" value="@(Model==null?"":Model.ValidateCode)" /><img src="@Url.Action("ValidateCode")" alt="验证码图片" title="看不清?点击更换" onclick="this.src = this.src+'?'"/></td><td>@Html.ValidationMessage("ValidateCode")@Html.ValidationMessage("validata")</td></tr>

邮箱验证码生成

邮件发送代码

public bool EmailValidataCode(int codeCount,out string content,string toMail){string code = CreateRandomCode(codeCount);content = code;try{MailMessage mail = new MailMessage();//发件人邮箱地址mail.From = new MailAddress("chenyan_it@");mail.To.Add(new MailAddress(toMail));mail.Subject = "【英莱特学员注册验证码】";mail.Body = $"您本次注册英莱特学员的验证码为【{code}】,如未进行注册操作请忽略!";//smtp邮件发送SmtpClient client = new SmtpClient();client.Host = "";client.EnableSsl = true;//授权码在每次开启邮箱的Smtp功能时自动生成的client.Credentials = new NetworkCredential("chenyan_it@", "ILPSMHKIVVQUQWLD");client.Send(mail);return true;}catch (Exception ex){return false;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。