前端調用
html代碼
<div>
<img id="imgcode" class="col-md-2 control-label" src="~/Login/GetValidateCode" alt="驗證碼" />
<a id="switchCode">換一張</a>
</div>
js代碼
<script type="text/javascript">
$("#switchCode").click(function () {
$("#imgcode").attr("src", "/Login/GetValidateCode?" + Math.random())
})
$("#imgcode").click(function () {
$("#imgcode").attr("src", "/Login/GetValidateCode?" + Math.random())
})
</script>
控制器
public ActionResult Index()
{
return View();
}
/// <summary>
/// 獲取驗證碼
/// </summary>
/// <returns></returns>
public ActionResult GetValidateCode()
{
VerifyCode vc = new VerifyCode();
//byte[] bytes = vc.GetVerifyCode();
return File(vc.GetVerifyCode(), @"image/jpeg");
}
驗證碼圖片生成類
/// <summary>
/// 驗證碼生成
/// </summary>
public class VerifyCode
{
public byte[] GetVerifyCode()
{
int codeW = 80;//寬
int codeH = 20;//高
int fontSize = 16;//大小
string chkCode = string.Empty;
//顏色列表,用於驗證碼的噪線、噪點
Color[] color = { Color.Black, Color.Red, Color.Green, Color.Blue, Color.Brown, Color.DarkBlue };
//生成驗證碼字符數組
char[] character = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'e', 'f', 'g', 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
//驗證碼字體設定
string[] font = { "Times New Roman" };
//隨機
Random rnd = new Random();
//生成驗證碼
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
//加密驗證碼寫入
WebHelper.WriteSession("nfine_session_verifycode", Md5.Equals(chkCode.ToLower(), 16));
//創建畫布
Bitmap bmp = new Bitmap(codeW, codeH);//設置寬高
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);//背景白
//畫噪點噪線
for (int i = 0; i < 3; i++)
{
//噪線範圍
int x1 = rnd.Next(codeW);
int y1 = rnd.Next(codeH);
int x2 = rnd.Next(codeW);
int y2 = rnd.Next(codeH);
Color clr = color[rnd.Next(color.Length)];
//寫到畫布
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//畫驗證碼
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, fontSize);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, (float)0);
}
//將驗證碼寫入內存流
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
catch (Exception)
{
return null;
}
finally
{
g.Dispose();
bmp.Dispose();
}
}
}
WebHelper
/// <summary>
/// T:Session鍵值類型
/// key:Session鍵名
/// value:Session鍵值
/// </summary>
/// <typeparam name="T">鍵值類型</typeparam>
/// <param name="key">鍵名</param>
/// <param name="value">鍵值</param>
public static void WriteSession<T>(string key, T value)
{
if(key.IsEmpty())
{
return;
}
HttpContext.Current.Session[key] = value;
}
Md5加密
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MusicStore.Models
{
public class Md5
{
/// <summary>
/// MD5加密
/// </summary>
/// <param name="str">加密字符串</param>
/// <param name="code">加密位數</param>
/// <returns></returns>
public static string md5(string str,int code)
{
string strEncrypt = string.Empty;
if(code==16)
{
strEncrypt = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").Substring(8, 16);
}
if (code == 12)
{
strEncrypt = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
}
return strEncrypt;
}
}
}