.NET MVC 使用验证码验证登录
2013-04-25 10:10
281 查看
无图无真像,首先截出效果图:
代码:
<div style="margin-top: 15px;"> <label for="m"> 验证码:</label> <input name="memVerifi" style="width: 65px;" type="text" class="easyui-numberbox" data-options="required:true,validType:''" /> <img id="imgVerifi" title="单击我可以换一张验证码" src="/Login/VerificationCode" onclick="changecode()" /> </div>
function changecode() { $('#imgVerifi').attr('src', '/Login/VerificationCode?t=' + new Date().getSeconds()); }
逻辑啥的就不说了,直接代码
控制器代码:
public FileResult VerificationCode() { var vc = new FortRun.Lib.Util.VerificationCode(); System.IO.MemoryStream ms = vc.CreateCheckCodeImage(); byte[] bytes = ms.ToArray(); return File(bytes, @"image/gif"); }
VerificationCode类代码:(注:生成图片代码系网上下载)
using System; using System.Drawing; namespace FortRun.Lib.Util { public class VerificationCode { private string GenerateCheckCode() { //创建整型型变量 int number; //创建字符型变量 char code; //创建字符串变量并初始化为空 string checkCode = String.Empty; //创建Random对象 Random random = new Random(); //使用For循环生成4个数字 for (int i = 0; i < 4; i++) { //生成一个随机数 number = random.Next(); //将数字转换成为字符型 code = (char)('0' + (char)(number % 10)); checkCode += code.ToString(); } //将生成的随机数添加到Session中 System.Web.HttpContext.Current.Session["VerificationCode"] = checkCode; //返回字符串 return checkCode; } public System.IO.MemoryStream CreateCheckCodeImage() { string checkCode = GenerateCheckCode(); //判断字符串不等于空和null if (checkCode == null || checkCode.Trim() == String.Empty) { System.Web.HttpContext.Current.Session["VerificationCode"] = null; return null; } //创建一个位图对象 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); //创建Graphics对象 Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的背景噪音线 for (int i = 0; i < 2; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2); } Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold)); var brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString(checkCode, font, brush, 2, 2); //画图片的前景噪音点 for (int i = 0; i < 100; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //画图片的边框线 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); //将图片输出到页面上 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); return ms; } finally { g.Dispose(); image.Dispose(); } } } }
验证端代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using FortRun.Model; namespace FortRun.BLL { public class LoginHelper { private string UserName { get; set; } private string UserPwd { get; set; } private string VerifiCode { get; set; } public LoginHelper(string username, string userpwd) { UserName = username; UserPwd = FortRun.Lib.Util.MD5.MD5Encrypt(userpwd, 32); } public LoginHelper(string username, string userpwd, string verificode) { UserName = username; UserPwd = FortRun.Lib.Util.MD5.MD5Encrypt(userpwd, 32); VerifiCode = verificode; } public bool CheckLogin(ref string msg) { if (!string.IsNullOrEmpty(VerifiCode)) { var verificode = System.Web.HttpContext.Current.Session["VerificationCode"] as string; if (verificode != VerifiCode) { msg = "验证码不匹配"; return false; } } if (msg == null) throw new ArgumentNullException("msg"); //do some check msg = "B1AC38B1-AA8E-4187-AB94-67CA809DE505"; if (UserName != "fortrun-001") { msg = "用户名错误"; return false; } if (UserName == "fortrun-001" && FortRun.Lib.Util.MD5.MD5Encrypt("fortrun-pwd", 32) != UserPwd) { msg = "密码不匹配"; return false; } var m = new M_Member { memCellPhone = "15888888888", memID = "10001", memIdNum = "362422xxxxxxxx8111", memName = "xxx", memRegisterTime = DateTime.Now.ToString("yyyy-MM-dd"), memSex = "xxxx", memStatus = "OK" }; System.Web.HttpContext.Current.Session["UserInfo"] = m; return true; } } }
很简单,可能有新手需要吧,我以前只知道从网上索取,从未为网上写过点什么。现在自己懂一点就写一点吧。
以上代码测试过
相关文章推荐
- .net MVC使用Session验证用户登录(4)
- .net MVC使用IPrincipal进行Form登录即权限验证(3)
- 【4】 .net MVC使用Session验证用户登录
- 使用shiro的的表单过滤器重写shiro默认的认证规则来实现先验证验证码再验证登录所遇到的问题
- 【3】.net MVC 使用IPrincipal进行Form登录即权限验证
- 如何在用户注册或登录时使用验证码图片
- jmeter 使用时间戳去redis获取验证码实现有验证码登录接口测试
- 使用用户名和密码验证apache登录
- JSF使用登录验证码
- 使用cookie绕过验证码进行模拟登录
- SQLSERVER误删Windows登录用户验证方式使用Windows身份验证的解决方法
- jsp使用servlet实现用户登录 及动态验证码
- 使用jquery1.2.6验证用户登录和注册总结
- php登录验证(包括用户名、密码、验证码、数据库、是否已登陆验证、自动登录和注销登录)
- 使用C#登录带验证码的网站
- 接口测试入门(3)--使用httpClient进行登录用例操作/set-cookies验证/ List<NameValuePair>设置post参数/json解析
- .net mvc 中的用户登录验证过滤器
- js实现登录界面验证码生成及验证
- MVC使用极验验证制作登录验证码学习笔记7
- 使用SpringSecurity3用户验证(异常信息,验证码)