随机六位数验证码生成
//一般处理程序里边的
//生成验证码字符串
var strCode = "0123456789PLMKOIJNBHUYGVCFTRDXZSEWAQmlpokijnbhuygvcftrdxzsewaq";
var Vcode = "";
//随机数
Random random = new Random();
for (int i = 0; i < 4; i++)
{
Vcode += strCode.Substring(random.Next(0,strCode.Length),1);
}
//用Cookie保存验证码 cookie记录信息 cookie存储在客户端(浏览器上)
//context.Response.Cookies.Add(new HttpCookie("code",Vcode));
//context.Response.Cookies.Add(new HttpCookie("code",Vcode));
context.Response.Cookies.Add(new HttpCookie("code",Vcode));
//生成图片步骤
//画板
using (Bitmap bitmap=new Bitmap(90,35))
{
//画布
//Graphics graphics = Graphics.FromImage(bitmap);
Graphics graphics = Graphics.FromImage(bitmap);
//画布填充背景
//graphics.FillRectangle(new SolidBrush(Color.YellowGreen),0,0,90,35);
graphics.FillRectangle(new SolidBrush(Color.YellowGreen),0,0,90,35);
//定义字体
//Font font = new Font("微软雅黑",18.0f);
Font font = new Font("微软雅黑",18.0f);
//画图
graphics.DrawString(Vcode,font,new SolidBrush(Color.White),5,5);
//像浏览器输出图片流
bitmap.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.ContentType = "image/jpeg";
}
using (Bitmap bitmap=new Bitmap(90,35))
{
//画布
Graphics graphics = Graphics.FromImage(bitmap);
//填充
graphics.FillRectangle(new SolidBrush(Color.YellowGreen),0,0,90,35);
//字体
Font font = new Font("微软雅黑",18.0f);
//画图
graphics.DrawString(Vcode,font,new SolidBrush(Color.White),5,5);
//保存
bitmap.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.ContentType = "image/jpeg";
}
context.Response.End();
}
//前台MVC里边的
<script>
function GetCookie(cname) {
var name = cname + "=";
var c = document.cookie.split(";");
for (var i = 0; i < c.length; i++) {
var ca = c[i].trim();
if (ca.indexOf(name) == 0) {
return ca.substring(name.length, ca.length);
}
}
return "";
}
$(function () {
$("#Login").click(function () {
//获取验证码
var YZM = $("#YZM").val();
//获取用户名
var UserName = $("#UserName").val();
//密码
var PassWord = $("#PassWord").val();
if (UserName.trim() == "") {
alert("请输入用户名");
return;
}
else if (PassWord.trim()=="") {
alert("请输入密码");
return;
}
else if (GetCookie("code") == "" || YZM=="") {
alert("请输入验证码");
return;
}
else if (YZM != GetCookie("code")) {
alert("输入验证码不正确");
return;
}
$.ajax({
url: "http://localhost:59729/api/login",
data: { account: UserName, pass: PassWord },
dataType: "json",
type: "Get",
success: function (data) {
if (data == "") {
alert("账号或密码错误");
}
else {
alert("登陆成功");
document.cookie = "d=" + data.data;
document.cookie = "displayName=" + data.DisplayName;
document.cookie = "account=" + data.Account;
document.cookie = "sex=" + data.Sex;
document.cookie = "DeptName=" + data.DeptName;
location.href = '/Home/Insert';
}
}
})
})
})
</script>
<div>
<input id="UserName" placeholder="请输入用户名" type="text" />
<br />
<input id="PassWord" placeholder="请输入登陆密码" type="text" />
<br />
<input id="YZM" placeholder="请输入验证码" type="text" />
<img src="~/Handler/Handler1.ashx" onclick="this.src = this.src + '?' + Math.random()" />
<br />
<input id="Login" type="button" value="登陆" style="cursor:pointer" />
</div>