GenerateValidateCode.cs 生成一个随机的验证字符串
using
System;
using System.Collections.Generic;
using System.Text;
namespace Ido.Security
... {
public class GenerateValidateCode
...{
Construction#region Construction
public GenerateValidateCode()
...{
this.GenerateCodingScope();
}
public GenerateValidateCode(byte codingLength)
...{
this.codingLength = codingLength;
this.GenerateCodingScope();
}
public GenerateValidateCode(ValidateCodingType codingType, byte codingLength)
...{
this.codingType = codingType;
this.codingLength = codingLength;
this.GenerateCodingScope();
}
#endregion
GenerateCodingScope#region GenerateCodingScope
/**//// <summary>
/// 设置CodingScope属性
/// </summary>
protected void GenerateCodingScope()
...{
switch (this.codingType)
...{
case ValidateCodingType.Numeral:
this.codingScope = numeralCodes;
break;
case ValidateCodingType.Letter:
this.codingScope = letterCodes;
break;
case ValidateCodingType.MixNumeralLetter:
this.codingScope = mixNumeralLetterCodes;
break;
default:
throw new ArgumentOutOfRangeException("CodingType", "字符编码的类型超出定义的区值范围。");
}
}
#endregion
StaticConfig#region StaticConfig
/**//// <summary>
/// 数字字符列表
/// </summary>
private static char[] numeralCodes = new char[] ...{ '1','2','3','4','5','6','7','8','9','0' };
/**//// <summary>
/// 字母字符列表
/// </summary>
private static char[] letterCodes = 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' };
/**//// <summary>
///
/// </summary>
private static char[] mixNumeralLetterCodes = new char[] ...{ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'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'};
#endregion
CodingType#region CodingType
private ValidateCodingType codingType = ValidateCodingType.MixNumeralLetter;
/**//// <summary>
/// 验证字符编码方式
/// </summary>
public ValidateCodingType CodingType
...{
get ...{ return codingType; }
set ...{ codingType = value; }
}
#endregion
CodingLength#region CodingLength
private byte codingLength = 4;
/**//// <summary>
/// 验证字符的字符长度
/// </summary>
public byte CodingLength
...{
get ...{ return codingLength; }
set
...{
if (value > 4)
...{
codingLength = value;
}
}
}
#endregion
CodingScope#region CodingScope
private char[] codingScope;
/**//// <summary>
/// 验证字符的范围
/// </summary>
public char[] CodingScope
...{
get ...{ return codingScope; }
set ...{ codingScope = value; }
}
#endregion
ValidateCode#region ValidateCode
/**//// <summary>
/// 验证字符串
/// </summary>
public string ValidateCode
...{
get
...{
return getValidateCode();
}
}
#endregion
getValidateCode#region getValidateCode
protected string getValidateCode()
...{
if (this.codingScope != null && this.codingScope.Length > 0)
...{
StringBuilder codeBuilder = new StringBuilder();
Random random = new Random();
for (int i = 0; i < this.codingLength; i++ )
...{
codeBuilder.Append(this.codingScope[random.Next(codingScope.Length)]);
}
return codeBuilder.ToString();
}
else
...{
throw new ArgumentException("属性CodingScope取值错误,CodingScope不能为Null或数组长度为0。",
"CodingScope");
}
}
#endregion
}
public enum ValidateCodingType:byte
...{
Numeral = 1,
Letter = 2,
MixNumeralLetter = 3,
}
}
using System.Collections.Generic;
using System.Text;
namespace Ido.Security
... {
public class GenerateValidateCode
...{
Construction#region Construction
public GenerateValidateCode()
...{
this.GenerateCodingScope();
}
public GenerateValidateCode(byte codingLength)
...{
this.codingLength = codingLength;
this.GenerateCodingScope();
}
public GenerateValidateCode(ValidateCodingType codingType, byte codingLength)
...{
this.codingType = codingType;
this.codingLength = codingLength;
this.GenerateCodingScope();
}
#endregion
GenerateCodingScope#region GenerateCodingScope
/**//// <summary>
/// 设置CodingScope属性
/// </summary>
protected void GenerateCodingScope()
...{
switch (this.codingType)
...{
case ValidateCodingType.Numeral:
this.codingScope = numeralCodes;
break;
case ValidateCodingType.Letter:
this.codingScope = letterCodes;
break;
case ValidateCodingType.MixNumeralLetter:
this.codingScope = mixNumeralLetterCodes;
break;
default:
throw new ArgumentOutOfRangeException("CodingType", "字符编码的类型超出定义的区值范围。");
}
}
#endregion
StaticConfig#region StaticConfig
/**//// <summary>
/// 数字字符列表
/// </summary>
private static char[] numeralCodes = new char[] ...{ '1','2','3','4','5','6','7','8','9','0' };
/**//// <summary>
/// 字母字符列表
/// </summary>
private static char[] letterCodes = 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' };
/**//// <summary>
///
/// </summary>
private static char[] mixNumeralLetterCodes = new char[] ...{ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'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'};
#endregion
CodingType#region CodingType
private ValidateCodingType codingType = ValidateCodingType.MixNumeralLetter;
/**//// <summary>
/// 验证字符编码方式
/// </summary>
public ValidateCodingType CodingType
...{
get ...{ return codingType; }
set ...{ codingType = value; }
}
#endregion
CodingLength#region CodingLength
private byte codingLength = 4;
/**//// <summary>
/// 验证字符的字符长度
/// </summary>
public byte CodingLength
...{
get ...{ return codingLength; }
set
...{
if (value > 4)
...{
codingLength = value;
}
}
}
#endregion
CodingScope#region CodingScope
private char[] codingScope;
/**//// <summary>
/// 验证字符的范围
/// </summary>
public char[] CodingScope
...{
get ...{ return codingScope; }
set ...{ codingScope = value; }
}
#endregion
ValidateCode#region ValidateCode
/**//// <summary>
/// 验证字符串
/// </summary>
public string ValidateCode
...{
get
...{
return getValidateCode();
}
}
#endregion
getValidateCode#region getValidateCode
protected string getValidateCode()
...{
if (this.codingScope != null && this.codingScope.Length > 0)
...{
StringBuilder codeBuilder = new StringBuilder();
Random random = new Random();
for (int i = 0; i < this.codingLength; i++ )
...{
codeBuilder.Append(this.codingScope[random.Next(codingScope.Length)]);
}
return codeBuilder.ToString();
}
else
...{
throw new ArgumentException("属性CodingScope取值错误,CodingScope不能为Null或数组长度为0。",
"CodingScope");
}
}
#endregion
}
public enum ValidateCodingType:byte
...{
Numeral = 1,
Letter = 2,
MixNumeralLetter = 3,
}
}
GenerateValidateImage.cs
using
System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Ido.Security
... {
public class GenerateValidateImage
...{
Construction#region Construction
public GenerateValidateImage(string validateCode)
...{
this.validateCode = validateCode;
Initialize();
}
public GenerateValidateImage(string validateCode, int imageWidth, int imageHeight)
...{
this.validateCode = validateCode;
width = imageWidth;
height = imageHeight;
//重新初始化
graphics.Dispose();
image.Dispose();
graphics = null;
image = null;
Initialize();
}
#endregion
Initialize#region Initialize
/**//// <summary>
/// 类初始化
/// </summary>
protected void Initialize()
...{
if (image == null)
...{
image = new Bitmap(width, height);
}
if(graphics == null)
...{
graphics = Graphics.FromImage(image);
}
}
#endregion
PrivateField#region PrivateField
private string validateCode;
private static Random random = new Random();
private static FontFamily[] fontFamilies = FontFamily.Families;
private static int fontSize = 12;
private static Bitmap image = null;
private static Graphics graphics = null;
#endregion
FontRgbMaxValue#region FontRgbMaxValue
private static int fontRgbMaxValue = 0x88;
public static int FontRgbMaxValue
...{
get ...{ return fontRgbMaxValue; }
set ...{ fontRgbMaxValue = value; }
}
#endregion
FontRgbMinValue#region FontRgbMinValue
private static int fontRgbMinValue = 0x22;
public static int FontRgbMinValue
...{
get ...{ return fontRgbMinValue; }
set ...{ fontRgbMinValue = value; }
}
#endregion
InterfereRgbMaxValue#region InterfereRgbMaxValue
private static int interfereRgbMaxValue = 0xbb;
public static int InterfereRgbMaxValue
...{
get ...{ return interfereRgbMaxValue; }
set ...{ interfereRgbMaxValue = value; }
}
#endregion
InterfereRgbMinValue#region InterfereRgbMinValue
private static int interfereRgbMinValue = 0x11;
public static int InterfereRgbMinValue
...{
get ...{ return interfereRgbMinValue; }
set ...{ interfereRgbMinValue = value; }
}
#endregion
BackGroundRgbMaxValue#region BackGroundRgbMaxValue
private static int backGroundRgbMaxValue = 0xff;
public static int BackGroundRgbMaxValue
...{
get ...{ return backGroundRgbMaxValue; }
set ...{ backGroundRgbMaxValue = value; }
}
#endregion
BackGroundRgbMinValue#region BackGroundRgbMinValue
private static int backGroundRgbMinValue = 0xaa;
public static int BackGroundRgbMinValue
...{
get ...{ return backGroundRgbMinValue; }
set ...{ backGroundRgbMinValue = value; }
}
#endregion
GetRgbColor#region GetRgbColor
protected Color GetRgbColor(int minValue, int maxValue)
...{
int alpha = 0xff << 24;
int redValue = random.Next(minValue, maxValue) << 16;
int greenValue = random.Next(minValue, maxValue) << 8;
int blueValue = random.Next(minValue, maxValue);
int rgbColor = alpha + redValue + greenValue + blueValue;
return Color.FromArgb(rgbColor);
}
#endregion
Height#region Height
private static int height = 24;
public static int Height
...{
get ...{ return GenerateValidateImage.height; }
set ...{ GenerateValidateImage.height = value; }
}
#endregion
Width#region Width
private static int width = 90;
public static int Width
...{
get ...{ return GenerateValidateImage.width; }
set ...{ GenerateValidateImage.width = value; }
}
#endregion
GetFontStyle#region GetFontStyle
/**//// <summary>
/// 设置字体样式
/// </summary>
protected Font GetFontStyle()
...{
return new Font(GetFontFamily(), fontSize, FontStyle.Italic);
}
#endregion
GetFontFamily#region GetFontFamily
/**//// <summary>
/// 随机获取一种系统字体
/// </summary>
/// <returns></returns>
protected FontFamily GetFontFamily()
...{
return fontFamilies[random.Next(fontFamilies.Length)];
}
#endregion
GenerateInterfere#region GenerateInterfere
/**//// <summary>
/// 在图象上写入干扰
/// </summary>
protected void GenerateInterfere()
...{
}
#endregion
DrawValidateCode#region DrawValidateCode
/**//// <summary>
/// 在图象上书写验证字符
/// </summary>
protected void DrawValidateCode()
...{
graphics.Clear(GetRgbColor(backGroundRgbMinValue, backGroundRgbMaxValue));
Brush brush = new SolidBrush(GetRgbColor(fontRgbMinValue, fontRgbMaxValue));
graphics.DrawString(this.validateCode, GetFontStyle(), brush, 3, 3 );
}
#endregion
GetImageStream#region GetImageStream
/**//// <summary>
/// 获取验证图象输出流
/// </summary>
/// <returns></returns>
public MemoryStream GetImageStream()
...{
DrawValidateCode();
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
return stream;
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Ido.Security
... {
public class GenerateValidateImage
...{
Construction#region Construction
public GenerateValidateImage(string validateCode)
...{
this.validateCode = validateCode;
Initialize();
}
public GenerateValidateImage(string validateCode, int imageWidth, int imageHeight)
...{
this.validateCode = validateCode;
width = imageWidth;
height = imageHeight;
//重新初始化
graphics.Dispose();
image.Dispose();
graphics = null;
image = null;
Initialize();
}
#endregion
Initialize#region Initialize
/**//// <summary>
/// 类初始化
/// </summary>
protected void Initialize()
...{
if (image == null)
...{
image = new Bitmap(width, height);
}
if(graphics == null)
...{
graphics = Graphics.FromImage(image);
}
}
#endregion
PrivateField#region PrivateField
private string validateCode;
private static Random random = new Random();
private static FontFamily[] fontFamilies = FontFamily.Families;
private static int fontSize = 12;
private static Bitmap image = null;
private static Graphics graphics = null;
#endregion
FontRgbMaxValue#region FontRgbMaxValue
private static int fontRgbMaxValue = 0x88;
public static int FontRgbMaxValue
...{
get ...{ return fontRgbMaxValue; }
set ...{ fontRgbMaxValue = value; }
}
#endregion
FontRgbMinValue#region FontRgbMinValue
private static int fontRgbMinValue = 0x22;
public static int FontRgbMinValue
...{
get ...{ return fontRgbMinValue; }
set ...{ fontRgbMinValue = value; }
}
#endregion
InterfereRgbMaxValue#region InterfereRgbMaxValue
private static int interfereRgbMaxValue = 0xbb;
public static int InterfereRgbMaxValue
...{
get ...{ return interfereRgbMaxValue; }
set ...{ interfereRgbMaxValue = value; }
}
#endregion
InterfereRgbMinValue#region InterfereRgbMinValue
private static int interfereRgbMinValue = 0x11;
public static int InterfereRgbMinValue
...{
get ...{ return interfereRgbMinValue; }
set ...{ interfereRgbMinValue = value; }
}
#endregion
BackGroundRgbMaxValue#region BackGroundRgbMaxValue
private static int backGroundRgbMaxValue = 0xff;
public static int BackGroundRgbMaxValue
...{
get ...{ return backGroundRgbMaxValue; }
set ...{ backGroundRgbMaxValue = value; }
}
#endregion
BackGroundRgbMinValue#region BackGroundRgbMinValue
private static int backGroundRgbMinValue = 0xaa;
public static int BackGroundRgbMinValue
...{
get ...{ return backGroundRgbMinValue; }
set ...{ backGroundRgbMinValue = value; }
}
#endregion
GetRgbColor#region GetRgbColor
protected Color GetRgbColor(int minValue, int maxValue)
...{
int alpha = 0xff << 24;
int redValue = random.Next(minValue, maxValue) << 16;
int greenValue = random.Next(minValue, maxValue) << 8;
int blueValue = random.Next(minValue, maxValue);
int rgbColor = alpha + redValue + greenValue + blueValue;
return Color.FromArgb(rgbColor);
}
#endregion
Height#region Height
private static int height = 24;
public static int Height
...{
get ...{ return GenerateValidateImage.height; }
set ...{ GenerateValidateImage.height = value; }
}
#endregion
Width#region Width
private static int width = 90;
public static int Width
...{
get ...{ return GenerateValidateImage.width; }
set ...{ GenerateValidateImage.width = value; }
}
#endregion
GetFontStyle#region GetFontStyle
/**//// <summary>
/// 设置字体样式
/// </summary>
protected Font GetFontStyle()
...{
return new Font(GetFontFamily(), fontSize, FontStyle.Italic);
}
#endregion
GetFontFamily#region GetFontFamily
/**//// <summary>
/// 随机获取一种系统字体
/// </summary>
/// <returns></returns>
protected FontFamily GetFontFamily()
...{
return fontFamilies[random.Next(fontFamilies.Length)];
}
#endregion
GenerateInterfere#region GenerateInterfere
/**//// <summary>
/// 在图象上写入干扰
/// </summary>
protected void GenerateInterfere()
...{
}
#endregion
DrawValidateCode#region DrawValidateCode
/**//// <summary>
/// 在图象上书写验证字符
/// </summary>
protected void DrawValidateCode()
...{
graphics.Clear(GetRgbColor(backGroundRgbMinValue, backGroundRgbMaxValue));
Brush brush = new SolidBrush(GetRgbColor(fontRgbMinValue, fontRgbMaxValue));
graphics.DrawString(this.validateCode, GetFontStyle(), brush, 3, 3 );
}
#endregion
GetImageStream#region GetImageStream
/**//// <summary>
/// 获取验证图象输出流
/// </summary>
/// <returns></returns>
public MemoryStream GetImageStream()
...{
DrawValidateCode();
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
return stream;
}
#endregion
}
}
Register.aspx
注册验证码:<asp:TextBox ID="TextBoxValidate" runat="server" Width="60"></asp:TextBox>
<img id="ValidateImage" src="ValidateImage.aspx" alt="" οnclick="this.src='ValidateImage.aspx?random=' + Math.random();" />
<a οnclick="document.getElementById('ValidateImage').src='ValidateImage.aspx?random=' + Math.random();">看不清,换一张</a>
ValidateImage.aspx.cs
protected
void
Page_Load(
object
sender, EventArgs e)
... {
GenerateValidateCode validate = new GenerateValidateCode(ValidateCodingType.MixNumeralLetter, 6);
string validateCode = validate.ValidateCode;
GenerateValidateImage image = new GenerateValidateImage(validateCode);
MemoryStream stream = image.GetImageStream();
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(stream.ToArray());
}
... {
GenerateValidateCode validate = new GenerateValidateCode(ValidateCodingType.MixNumeralLetter, 6);
string validateCode = validate.ValidateCode;
GenerateValidateImage image = new GenerateValidateImage(validateCode);
MemoryStream stream = image.GetImageStream();
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(stream.ToArray());
}