菜鸟问题...登陆验证码什么做?
做登陆系统的时候那验证码是什么实现的;
需要验证吗? 要什么验证?
请高手指点~~~
[解决办法]
防止暴力破解登陆
[解决办法]
//命名空间
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 ', 'k ', 'l ', 'm ', 'n ', 'o ', 'p ', 'q ', 'r ', 's ', 't ', 'u ', 'v ', 'w ', 'x ', 'y ', 'z ',
'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 '
};
public static string GenerateRandom(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
Random rd= new Random();
for(int i=0;i <Length;i++)
{
newRandom.Append(constant[rd.Next(62)]);
}
return newRandom.ToString();
}
[解决办法]
public string RandomNum(int n) //
{
string strchar = "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,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[] VcArray = strchar.Split( ', ');
string VNum = " "; //
int temp = -1; //记录上次随机数值,尽量避免产生几个一样的随机数
//采用一个简单的算法以保证生成随机数的不同
Random rand = new Random();
for (int i = 1; i < n + 1; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(61);
if (temp != -1 && temp == t)
{
return RandomNum(n);
}
temp = t;
VNum += VcArray[t];
}
return VNum;//返回生成的随机数
}
[解决办法]
/// <summary>
/// 验证码的类
/// </summary>
public class CreateImage
{
/// <summary>
/// 绘画调用的方法 一般都是在窗体加载事件上调用
/// </summary>
public static void DrawImage()
{
CreateImage img = new CreateImage();
HttpContext.Current.Session[ "CheckCode "]=img.RndNum(4);
img.CreateImages(HttpContext.Current.Session[ "CheckCode "].ToString());
}
private void CreateImages(string checkCode)
{
int iwidth = (int)(checkCode.Length * 20); //参数的长度的13倍
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 23);//重新设置宽度,高度为23
Graphics g = Graphics.FromImage(image);//重新绘画
g.Clear(Color.White);//把背景颜色重新设置
Color[] c = {Color.Black,Color.Red,Color.DarkBlue,Color.Green,Color.Orange,Color.Brown,Color.DarkCyan,Color.Purple,Color.Yellow,Color.Turquoise};//颜色的数组
string[] font = { "Verdana ", "Microsoft Sans Serif ", "Comic Sans MS ", "Arial ", "宋体 ", "黑体 "};//字体的数组
FontStyle[] fontStyle = {FontStyle.Bold,FontStyle.Italic,FontStyle.Regular,FontStyle.Strikeout,FontStyle.Underline};//字体形势 比如加粗 倾斜
Random rand = new Random();
for(int i=0;i <50;i++) //绘制50个黑点填充背景
{
int x = rand.Next(image.Width);//随机得到小于绘画出来图片的宽度数字
int y = rand.Next(image.Height);//随机得到小于绘画出来图片的高度的数字
g.DrawRectangle(new Pen(Color.LightGray, 0),x,y,2,1);//绘制矩形图案 其实是一个点
}
for(int i=0;i <checkCode.Length;i++)
{
int cindex = rand.Next(8);//颜色数组的长度为8
int findex = rand.Next(6);//字体数组的长度为6
int fsindex = rand.Next(5);//字体形势数组的长度为5
Font f = new Font(font[findex],11,fontStyle[fsindex]);//定义字体
Brush b = new System.Drawing.SolidBrush(c[cindex]);//定义一个刷子
int ii=4;//绘画字符串时的坐标
if((i+1)%3==0)
{
ii=1;
}
else if((i+1)%3==1)
{
ii=2;
}
else if((i+1)%3==2)
{
ii=3;
}
g.DrawString(checkCode.Substring(i,1), f, b, 3+(i*20), ii);//绘制一个字符的字符串
}
g.DrawRectangle(new Pen(Color.Black,0),0,0,image.Width-1,image.Height-1);//绘制一个矩形
System.IO.MemoryStream ms = new System.IO.MemoryStream();//创建流
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);//保存到内存中
HttpContext.Current.Response.ClearContent();//清楚缓存中所有内容的输出
HttpContext.Current.Response.ContentType = "image/Jpeg ";//设置输出流的类型
HttpContext.Current.Response.BinaryWrite(ms.ToArray());//已二进制的形势写入http输出流
g.Dispose();
image.Dispose();
}
private string RndNum(int VcodeNum)
{
string Vchar = "2,3,4,5,6,7,8,9,Q,W,E,R,T,Y,U,P,A,S,D,F,G,H,J,K,L,Z,X,C,V,B,N,M ";
string[] VcArray = Vchar.Split( ', ') ;
string VNum = " ";
int temp = -1;
Random rand =new Random();
for ( int i = 1 ; i < VcodeNum+1 ; i++ )
{
if ( temp != -1)
{
rand =new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(VcArray.Length );
if (temp != -1 && temp == t)
{
return RndNum( VcodeNum );
}
temp = t ;
VNum += VcArray[t];
}
return VNum ;
}
}
[解决办法]
就是说什么知道用户输入的验证吗是否正确!
--------------------------
把生成的验证码存到session或cookies里,如何判断用户输入的验证码是否和session或cookies里的值一样
[解决办法]
验证码在用户注册新用户的时候 用
防止暴力用程序注册比如像qq号 游戏帐号什么的 论坛用户验证也必不可少 不是马甲多阿
[解决办法]
http://hi.baidu.com/kmiaoer/blog/item/7b889d2222d905f3d6cae2d6.html
http://hi.baidu.com/kmiaoer/blog/item/4f18781e20b4f5f51ad576e8.html