热烈祝贺台州朗动科技的站长论坛隆重上线!(2012-05-28)    热烈庆祝伟大的祖国60周年生日 点击进来我们一起为她祝福吧(2009-09-26)    站长论坛禁止发布广告,一经发现立即删除。谢谢各位合作!.(2009-08-08)    热烈祝贺台州网址导航全面升级,全新版本上线!希望各位一如既往地支持台州网址导航的发展.(2009-03-28)    台州站长论坛恭祝各位新年快乐,牛年行大运!(2009-01-24)    台州Link正式更名为台州网址导航,专业做以台州网址为主的网址导航!(2008-05-23)    热烈祝贺台州Link资讯改名为中国站长资讯!希望在以后日子里得到大家的大力支持和帮助!(2008-04-10)    热烈祝贺台州Link论坛改名为台州站长论坛!希望大家继续支持和鼓励!(2008-04-10)    台州站长论坛原[社会琐碎]版块更名为[生活百科]版块!(2007-09-05)    特此通知:新台州站长论坛的数据信息全部升级成功!">特此通知:新台州站长论坛的数据信息全部升级成功!(2007-09-01)    台州站长论坛对未通过验证的会员进行合理的清除,请您谅解(2007-08-30)    台州网址导航|上网导航诚邀世界各地的网站友情链接和友谊联盟,共同引领网站导航、前进!(2007-08-30)    禁止发广告之类的帖,已发现立即删除!(2007-08-30)    希望各位上传与下载有用资源和最新信息(2007-08-30)    热烈祝贺台州站长论坛全面升级成功,全新上线!(2007-08-30)    
便民网址导航,轻松网上冲浪。
台州维博网络专业开发网站门户平台系统
您当前的位置: 首页 » ASP/ASP.NET编程 » ASP.NET随机码生成示例

ASP.NET随机码生成示例

论坛链接
  • ASP.NET随机码生成示例
  • 发布时间:2007-09-21 10:09:27    浏览数:6560    发布者:tznktg    设置字体【   
现在很多网页登陆的时候都使用了随机图片的方式,是一种简单、有效的防止黑客恶意攻击的手段。今天看了一些网上的资料,明白其生成原理:从样本中,获取随机字符串,随机字符串保存进session,并以位图的方式形成随机码图片。
实现:
添加命名空间

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
生成页代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

public partial class getRandImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//生成随机码图片
SetValidateCode();
//生成页面不保存到cache
Response.Cache.SetNoStore();
}

//设置验证码
private void SetValidateCode()
{
//新建位图
Bitmap newBitmap = new Bitmap(
71,
23,
PixelFormat.Format32bppArgb
);
//从位图获得绘图画面
Graphics g = Graphics.FromImage(newBitmap);
//随机数生成器
Random r = new Random();
//绘图画面清空
g.Clear(Color.White);
//绘图画面划线干扰
for (int i = 0; i < 50; i++)
{
int x1 = r.Next(newBitmap.Width);
int x2 = r.Next(newBitmap.Width);
int y1 = r.Next(newBitmap.Height);
int y2 = r.Next(newBitmap.Height);
g.DrawLine(new Pen(
Color.FromArgb(r.Next())),
x1,
y1,
x2,
y2
);
}
//绘图画面点数干扰
for (int i = 0; i < 100; i++)
{
int x = r.Next(newBitmap.Width);
int y = r.Next(newBitmap.Height);
newBitmap.SetPixel(
x,
y,
Color.FromArgb(r.Next())
);
}
//获得随机字符串(5位长度)
string value = GenerateRandom(5);
//随机字符串赋值给Session
Session["RandCode"] = value;
//定义图片显示字体样式
Font font = new Font(
"Arial",
14,
FontStyle.Bold
);
Random rr = new Random();
int yy = rr.Next(1, 4);
//定义随机字符串显示图片刷子
LinearGradientBrush brush = new LinearGradientBrush(
new Rectangle(0, 0, 71, 23),
Color.Red,
Color.Blue,
1.2f,
true
);
g.DrawString(value, font, brush, 2, yy);
g.DrawRectangle(new Pen(
Color.Silver),
0,
0,
70,
22
);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
newBitmap.Save(ms, ImageFormat.Gif);
//输出图片
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
}

//常量集
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'
};


//生成随机字符串
public static string GenerateRandom(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(36);
Random rd = new Random();
for (int i = 0; i < Length; i++)
{
newRandom.Append(constant[rd.Next(36)]);
}
return newRandom.ToString();
}
}

使用随机图片的页面,IMAGE控件的写法如下:

<asp:Image ID="Image1" ImageUrl="~/getRandImg.aspx" runat="server" />

示例代码:http://www.cnblogs.com/Files/heekui/RandCode.rar

http://www.cnblogs.com/heekui/archive/2007/01/06/613609.html
娱乐休闲专区A 影视预告B 音乐咖啡C 英语阶梯D 生活百科
网页编程专区E AMPZF HTMLG CSSH JSI ASPJ PHPK JSPL MySQLM AJAX
Linux技术区 N 系统管理O 服务器架设P 网络/硬件Q 编程序开发R 内核/嵌入
管理中心专区S 发布网址T 版主议事U 事务处理