日期:2014-05-18  浏览次数:20420 次

菜鸟问题...登陆验证码什么做?
做登陆系统的时候那验证码是什么实现的;
需要验证吗?   要什么验证?
请高手指点~~~

------解决方案--------------------
防止暴力破解登陆

------解决方案--------------------
//命名空间
using System.Security.Cryptography;

public class RandomImage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
//输出带有随机验证码的图片
this.CreateCheckCodeImage(this.GenerateCheckCode());
}

private string GenerateCheckCode()
{
//验证码长度
int CODELENGTH = 4;
int number;
string strCode = string.Empty;

Random r = new Random();

for(int i = 0; i < CODELENGTH; i++)
{
number = r.Next();

//字符从0~9, A~Z中随机产生,对应的ASCII码分别为48~57, 65~90
number = number % 36;
if(number < 10)
number += 48;
else
number += 55;

strCode += ((char)number).ToString();
}

//在Cookie中保存验证码
Response.Cookies.Add(new HttpCookie( "CheckCode ", strCode));
return strCode;
}

private void CreateCheckCodeImage(string checkCode)
{
//若验证码为空,则直接返回
if(checkCode == null || checkCode.Trim() == string.Empty)
return;

//根据验证码的长度确定输出图片的长度
System.Drawing.Bitmap image = new Bitmap((int)Math.Ceiling(checkCode.Length * 15), 20);

Graphics g = Graphics.FromImage(image);

try
{
Random r = new Random();

//清空图片背景色
g.Clear(Color.White);

//画图片的背景噪音线10条
for(int i = 0; i < 10; i ++)
{
int x1 = r.Next(image.Width);
int x2 = r.Next(image.Width);
int y1 = r.Next(image.Height);
int y2 = r.Next(image.Height);

//用银色画出噪音线
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}

Font f = new Font( "Arial ", 12, (FontStyle.Bold|FontStyle.Italic));

//线性渐变画刷
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Purple, 1.2f, true);
g.DrawString(checkCode, f, brush, 2, 2);

//画图片的前景噪音点50个
for(int i = 0; i < 50; i++)
{
int x = r.Next(image.Width);
int y = r.Next(image.Height);

image.SetPixel(x, y, Color.FromArgb(r.Next()));
}

//画图片的框线
g.DrawRectangle(new Pen(Color.SaddleBrown), 0, 0, image.Width - 1, image.Height - 1);

//创建内存流用于输出图片
using(System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
//图片格式制定为png
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

//清除缓冲区流中的所有输出
Response.ClearContent();

//输出流的HTTP MIME类型设置为 "image/Png "
Response.ContentType = "image/Png ";

//输出图片的二进制流
Response.BinaryWrite(ms.ToArray());
}
}
finally
{
//释放Bitmap对象和Graphics对象
g.Dispose();
image.Dispose();
}
}
------解决方案--------------------
private static char[] constant=
{
'0 ', '1 ', '2 ', '3 ', '4 ', '5 ', '6 ', '7 ', '8 ', '9 ',
'a ', 'b ', 'c ', 'd ', 'e ', 'f ', 'g ', 'h ', 'i ', 'j ', '