您的位置:首页 > 理论基础 > 计算机网络

使用HttpHandler输出验证码

2010-06-24 11:58 393 查看
C#生成随机验证码

PS:

验证码给用户带来的不友好体验已经越来越明显了。

多数网站也都已经开始尝试避免验证码的输入,比如:淘宝的登录,只有在多次登录错误的情况下,才会出现验证码。

眼瞅着验证码已经开始向幕后转移了,验证码会以这种方式落幕吗?

验证码的明天会是怎样呢?

欢迎留言~~

/// <summary>
/// 反回验证码图片,并保存文字于SESSION
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ValiCode : IHttpHandler, IRequiresSessionState
{
private const string _CODEITEM = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM123456789";
private const string _SAVESESSIONNAME = "ValiCode";
private readonly Random _Random = new Random();

// 可选字体
private readonly string[] _FamilyName = new string[] { "Arial", "Helvetica", "Geneva", "sans-serif", "Verdana" };
// 可选颜色
private readonly string[] _BrushName = new string[] { "OliveDrab", "ForestGreen", "DarkCyan", "LightSlateGray", "RoyalBlue", "SlateBlue", "DarkViolet", "MediumVioletRed", "IndianRed", "Firebrick", "Chocolate", "Peru", "Goldenrod" };
// 可选笔刷
private readonly Brush[] _BrushItems = new Brush[] { Brushes.OliveDrab, Brushes.ForestGreen, Brushes.DarkCyan, Brushes.LightSlateGray, Brushes.RoyalBlue, Brushes.SlateBlue, Brushes.DarkViolet, Brushes.MediumVioletRed, Brushes.IndianRed, Brushes.Firebrick, Brushes.Chocolate, Brushes.Peru, Brushes.Goldenrod };
// 可选字大小(面积)
private readonly int[] _FontSize = new int[] { 13, 14, 15, 16, 17, 18, 19, 20 };
// 开始绘制时X坐标位置
private int _NowPointX = 0;
// 开始绘制时Y坐标位置
private int _NowPointY = 0;
// 图片模糊点数量
private int _PixelCount = 100;
// 图片贝塞尔样条数量
private int _BezierCount = 2;

public void ProcessRequest(HttpContext context)
{
string code = GetRandomCode(4);

Bitmap map = GetRandomBitmap(80, 25, code);
map.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); // 将图片对象保存到输出流

context.Session[_SAVESESSIONNAME] = code; // 将验证文字保存于Session
context.Response.End();
}

// 生成验证码
private Bitmap GetRandomBitmap(int width, int height, string codeStr)
{
if (width < 1 || height < 1 || string.IsNullOrEmpty(codeStr))
{
return null;
}

Bitmap map = new Bitmap(width, height); // 创建图片
Graphics grap = Graphics.FromImage(map); // 创建画板
grap.Clear(Color.White); // 图片默认填充色
char[] code = codeStr.ToArray();

// 向画板中绘制文字
PointF point;
foreach (char citem in code)
{
point = new PointF(_NowPointX, _NowPointY);
grap.DrawString(citem.ToString(), GetRandomFont(), GetRandomBrush(), point);
}

// 向画板中绘制贝塞尔样条
int doInt = 0;
do
{
grap.DrawBezier(GetRandomPen(), GetRandomPoint(width, height), GetRandomPoint(width, height), GetRandomPoint(width, height), GetRandomPoint(width, height));
doInt++;
} while (doInt < _BezierCount);

grap.Dispose();

// 在图片中添加模糊点
doInt = 0;
do
{
map.SetPixel(_Random.Next(width), _Random.Next(height), Color.FromName(_BrushName[_Random.Next(0, _BrushName.Length - 1)]));
doInt++;
} while (doInt < _PixelCount);

return map;
}

// 在固定范围内返回随机坐标
private Point GetRandomPoint(int width, int height)
{
return new Point(_Random.Next(width), _Random.Next(height));
}
// 从填充对象数组中返回随机填充对象
private Brush GetRandomBrush()
{
return _BrushItems[_Random.Next(0, _BrushItems.Length - 1)];
}
// 从字体数组中返回随机字体
private Font GetRandomFont()
{
int size = _FontSize[_Random.Next(0, _FontSize.Length - 1)];
Font font = new Font(_FamilyName[_Random.Next(0, _FamilyName.Length - 1)], size);
_NowPointX += size;
return font;
}
// 返回随机画笔对象
private Pen GetRandomPen()
{
return new Pen(_BrushItems[_Random.Next(0, _BrushItems.Length - 1)]);
}

// 返回验证码字符串
private string GetRandomCode(int len)
{
if (len < 1)
{
return null;
}
StringBuilder randomStr = new StringBuilder();
char[] source = _CODEITEM.ToCharArray();
int beg = 0;
int end = source.Length - 1;

for (int i = 0; i < len; i++)
{
randomStr.Append(source[_Random.Next(beg, end)]); // 随机从字符数组中获取字符添加到StringBuilder对象中
}
return randomStr.ToString();
}

public bool IsReusable
{
get
{
return true;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: