无图无真像,首先截出效果图:
代码:
<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" οnclick="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;
}
}
}
以上代码测试过