验证码

验证码

/// <summary>
///负责生成验证码
/// </summary>
/// <returns></returns>
public ActionResult Vcode()
{
//1.产生一个随机数
string rVcode = GetVcode(1);
//2,将验证码存session中
Session[Keys.Vcode] = rVcode;
//3,将验证码画到图片上面返回
byte[] imgBuffer;
using (Image img = new Bitmap(65, 25))
{
using (Graphics g = Graphics.FromImage(img))
{
//以白色填充背景
g.Clear(Color.White);
//将验证码画到图片上面
g.DrawString(rVcode, new Font("黑体", 16, FontStyle.Bold | FontStyle.Italic | FontStyle.Strikeout), Brushes.Red, 0, 0);
}

//4将图片中的数据保存在imgBuffer数组中
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
imgBuffer = ms.ToArray();
}
}
return File(imgBuffer,"image/jpeg");
}
Random r = new Random();
private string GetVcode(int num)
{
string str = "23456789abcdefjhgkmnpqrstxyzABCDEFJHGKMNPQRSTXYZ";
string res = string.Empty;
int length = str.Length;
for (int i = 0; i < num; i++)
{
res += str[r.Next(length)];
}
return res;
}


---------------
//验证码上鼠标移入事件
function sho() {
var b = document.getElementById("b");
b.innerHTML = "点击切换验证码";
}
//验证码上鼠标移出事件
function hid() {
var b = document.getElementById("b");
b.innerHTML = "";
}
----------------
@using (Ajax.BeginForm("Login", "Login", new AjaxOptions() {
Url=@Url.Action("Login","Login"),
HttpMethod="post",
OnSuccess="success"
}))
{
<table class="list">
<tr>
<th>@Html.DisplayNameFor(c=>c.uLoginName)</th>
<td>
@Html.TextBoxFor(c=>c.uLoginName)
@Html.ValidationMessageFor(c=>c.uLoginName)
</td>
</tr>
<tr>
<th>@Html.DisplayNameFor(c=>c.uLoginPWD)</th>
<td>
@Html.PasswordFor(c=>c.uLoginPWD)
@Html.ValidationMessageFor(c=>c.uLoginPWD)
</td>
</tr>
<tr>
<th>@Html.DisplayNameFor(c=>c.Vcode)</th>
<td>
@Html.TextBoxFor(c => c.Vcode, new {style="100px" })
<img id="code" onclick="reCode()" onmouseover="sho();" onmouseout="hid();" src="/admin/Vcode/Vcode" style="65px;height:25px;cursor:pointer" />
<span id="b" style="color:#0094ff"></span>
@Html.ValidationMessageFor(c=>c.Vcode)
</td>
</tr>
<tr>
<th></th>
<td>
@Html.DisplayNameFor(c=>c.IsRemember)
@Html.CheckBoxFor(c=>c.IsRemember)
</td>
</tr>
<tr>
<th></th>
<td>
<input id="btlogin" style="display:none" type ="submit" value="登录" />
<input id="btreset" style="display:none" type ="reset" />
</td>
</tr>
</table>
}

人的本事不是与生俱来的,不是你掌握了多少,而是当你面对一个未知问题的时候,你能用多少时间来掌握!
原文地址:https://www.cnblogs.com/dianshen520/p/4349241.html