You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
7.8 KiB
Plaintext

11 months ago
using BP.Web;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using BP.Difference;
using BP.DA;
namespace BP.Tools
{
public class Verify
{
/// <summary>
/// 随机码认证
/// </summary>
/// <param name="code">生成认证长度</param>
public static string DrawImage(int code, string sessionName, string errorSign, string codeSign, string userNo)
{
string str = Rand.Number(5);
//Dictionary<string, string> cookieValues = new Dictionary<string, string>();
////base64编码会把+改为空格的问题修复
//cookieValues.Add(sessionName + codeSign, Convert.ToBase64String(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("+", "%2B"));
//cookieValues.Add(sessionName + errorSign, sessionName + errorSign);
//HttpContextHelper.ResponseCookieAdd(cookieValues, null, "CCS");
//// HttpContext Core中没又 session 使用 HttpContextHelper.SessionSet 替代
//HttpContextHelper.SessionSet(sessionName, str);
string atPara = DBAccess.RunSQLReturnString("select AtPara from wf_emp where no='" + userNo + "'");
AtPara ap = new AtPara(atPara);
//ap.SetVal(sessionName + errorSign, sessionName + errorSign);
//ap.SetVal(sessionName + codeSign, Convert.ToBase64String(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("+", "%2B"));
ap.SetVal(codeSign, Convert.ToBase64String(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("+", "%2B"));
DBAccess.RunSQL("update wf_emp set atPara='" + ap.GenerAtParaStrs() + "' where no='" + userNo + "'");
return CreateImages(str);
}
/// <summary>
/// /// 生成验证图片
/// /// </summary>
/// /// <param name="checkCode">验证字符</param>
private static string CreateImages(string checkCode)
{
int iwidth = (int)(checkCode.Length * 15);
Bitmap image = new Bitmap(iwidth, 25);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.LightCyan);
//定义颜色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
//定义字体
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体", "Comic Sans MS" };
Random rand = new Random();
//随机输出噪点
for (int i = 0; i < 150; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawPie(new Pen(Color.LightGray, 0), x, y, 6, 6, 1, 1);
}
//输出不同字体和颜色的验证码字符
for (int i = 0; i < checkCode.Length; i++)
{
int cindex = rand.Next(7);
int findex = rand.Next(6);
Font fs_font = new System.Drawing.Font(font[findex], 14, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(c[cindex]);
int ii = 4;
if ((i + 1) % 2 == 0)
{
ii = 2;
}
g.DrawString(checkCode.Substring(i, 1), fs_font, b, 3 + (i * 12), ii);
}
//画一个边框
g.DrawRectangle(new Pen(Color.Red, 0), 100, 0, image.Width - 1, image.Height - 1);
//输出到浏览器
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
return "data:image/jpg;base64," + Convert.ToBase64String(arr);
}
}
public class Rand
{
/// <summary>
/// 生成随机数字
/// </summary>
/// <param name="length">生成长度</param>
/// <returns></returns>
public static string Number(int Length)
{
return Str(Length, false);
}
/// <summary>
/// 生成随机数字
/// </summary>
/// <param name="Length">生成长度</param>
/// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
/// <returns></returns>
public static string Number(int Length, bool Sleep)
{
if (Sleep)
System.Threading.Thread.Sleep(2);
string result = "";
System.Random random = new Random();
for (int i = 0; i < Length; i++)
{
result += random.Next(10).ToString();
}
return result;
}
/// <summary>
/// 生成随机字母与数字
/// </summary>
/// <param name="IntStr">生成长度</param>
/// <returns></returns>
public static string Str(int Length)
{
return Str(Length, false);
}
/// <summary>
/// 生成随机字母与数字
/// </summary>
/// <param name="Length">生成长度</param>
/// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
/// <returns></returns>
public static string Str(int Length, bool Sleep)
{
if (Sleep)
System.Threading.Thread.Sleep(2);
char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
string result = "";
int n = Pattern.Length;
System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < Length; i++)
{
int rnd = random.Next(0, n);
result += Pattern[rnd];
}
return result;
}
/// <summary>
/// 生成随机纯字母随机数
/// </summary>
/// <param name="IntStr">生成长度</param>
/// <returns></returns>
public static string Str_char(int Length)
{
return Str_char(Length, false);
}
/// <summary>
/// 生成随机纯字母随机数
/// </summary>
/// <param name="Length">生成长度</param>
/// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
/// <returns></returns>
public static string Str_char(int Length, bool Sleep)
{
if (Sleep)
System.Threading.Thread.Sleep(2);
char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
string result = "";
int n = Pattern.Length;
System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < Length; i++)
{
int rnd = random.Next(0, n);
result += Pattern[rnd];
}
return result;
}
/// <summary>
/// MD5 16位加密
/// </summary>
/// <param name="ConvertString"></param>
/// <returns></returns>
public static string GetMd5Str(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
t2 = t2.Replace("-", "");
return t2;
}
}
}