.Net Core Session验证码

1、验证码帮助类

namespace IdeaCore.Services.Common
{
public class ValidateCodeService : IValidateCodeService
{
private const string ValidateCodeKey = "ValidateCode";
private const double Pi2 = 6.283185307179586476925286766559;
private const string BackColor = "#dde3ec"; /// <summary>
/// 获取验证码的sessionkey
/// </summary>
/// <returns></returns>
public string GetValidateCodeKey() {
return ValidateCodeKey;
} /// <summary>
/// 生成验证码
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public string CreateValidateCode(int length)
{
string code = string.Empty; while (true)
{
var s = (Guid.NewGuid() + "").Substring(, );
if (s == "" || s == "l" || s == "o" || s == "")
{ }
else
{
code += s;
} if (code.Length >= length)
{
break;
}
}
return code;
} /// <summary>
/// 创建验证码的图片
/// </summary>
/// <param name="validateCode">验证码</param>
public byte[] CreateValidateGraphic(string validateCode)
{
const int fSize = ;
const int fWidth = fSize + ; int imageWidth = validateCode.Length * fWidth + + * ;
const int imageHeight = fSize * + ; Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
string[] fonts = { "Arial", "Georgia" }; var image = new Bitmap(imageWidth, imageHeight);
Graphics g = Graphics.FromImage(image);
try
{
g.Clear(ColorTranslator.FromHtml(BackColor));
var rand = new Random(); ////给背景添加随机生成的燥点
//Pen pen = new Pen(Color.RoyalBlue, 0);
// int c = 6 * 20; //杂点数量
// for (int i = 0; i < c; i++)
// {
// int x = rand.Next(image.Width);
// int y = rand.Next(image.Height);
// g.DrawRectangle(pen, x, y, 1, 1);
// } const int n1 = (imageHeight - - * );
const int n2 = n1 / ;
const int top1 = n2;
const int top2 = n2 * ; //随机字体和颜色的验证码字符
for (int i = ; i < validateCode.Length; i++)
{
int cindex = rand.Next(colors.Length - );
int findex = rand.Next(fonts.Length - ); var f = new Font(fonts[findex], fSize, FontStyle.Bold);
Brush b = new SolidBrush(colors[cindex]); var top = i % == ? top2 : top1; var left = i * fWidth;
if (i == ) { left += ; } g.DrawString(validateCode.Substring(i, ), f, b, left, top);
}
//
image = TwistImage(image, true, new Random().Next(, ), new Random().Next(, )); var stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
//输出图片流
return stream.ToArray();
}
finally
{
g.Dispose();
image.Dispose();
}
} #region 产生波形滤镜效果 /// <summary>
/// 正弦曲线Wave扭曲图片(Edit By 51aspx.com)
/// </summary>
/// <param name="srcBmp">图片路径</param>
/// <param name="bXDir">如果扭曲则选择为True</param>
/// <param name="dMultValue"></param>
/// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
/// <returns></returns>
private static Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
{
var destBmp = new Bitmap(srcBmp.Width, srcBmp.Height); // 将位图背景填充为白色
var graph = Graphics.FromImage(destBmp);
graph.FillRectangle(new SolidBrush(ColorTranslator.FromHtml(BackColor)), , , destBmp.Width, destBmp.Height);
graph.Dispose(); double dBaseAxisLen = bXDir ? destBmp.Height : destBmp.Width; for (int i = ; i < destBmp.Width; i++)
{
for (int j = ; j < destBmp.Height; j++)
{
var dx = bXDir ? (Pi2 * j) / dBaseAxisLen : (Pi2 * i) / dBaseAxisLen;
dx += dPhase;
var dy = Math.Cos(dx); // 取得当前点的颜色
int nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
int nOldY = bXDir ? j : j + (int)(dy * dMultValue); var color = srcBmp.GetPixel(i, j);
if (nOldX >= && nOldX < destBmp.Width
&& nOldY >= && nOldY < destBmp.Height)
{
destBmp.SetPixel(nOldX, nOldY, color);
}
}
} return destBmp;
}
#endregion
}
}

2、获取验证码

/// <summary>
/// 获取验证码
/// </summary>
/// <returns></returns>
public FileContentResult GetValidateCode()
{
var code = _validateCodeService.CreateValidateCode();
var bytes = _validateCodeService.CreateValidateGraphic(code);
HttpContext.Session.SetString(_validateCodeService.GetValidateCodeKey(), code);
return File(bytes, @"image/jpeg");
}

3、验证验证码

/// <summary>
/// 验证验证码
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
protected bool ValidateCode(string code) {
if (string.IsNullOrEmpty(code)) {
return false;
} var sessionCode = HttpContext.Session.GetString("ValidateCode");
return sessionCode != null && sessionCode == code.Trim();
}
上一篇:javascrpit开发连连看记录-小游戏


下一篇:详解JavaScript中的事件处理