All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP分页实例:用PHP实现网页开发中的翻页跳转_[PHP教程]
无废话C#设计模式之二:Singleton_.net资料_编程技术
PHP session常见问题集锦及解决办法_[PHP教程]
delphi常用的键盘事件处理方法-实例
ASP.NET技巧:字符自动截取问题_.net资料_编程技术
AJAX实例:根据邮编自动完成地址信息_[AJAX教程]
asp.net初学者:petshop4.0设计说明_.net资料_编程技术
理论:论C#变得越来越臃肿是不可避免的_.net资料_编程技术
ASP.NET,MVC+LINQ开发一个图书销售站点(7)-图书分类管理_[Asp.Net教程]
SQL,Server之分布式事务_mssql学习_编程技术
Reading number is top 10 pictures
初五接财神啦!五路财神齐来到
各种囧况!玩游戏最不喜欢出现的十件事(点评)
Small s breast enhancement demonstration
A man's favorite things11
西游日记1
Summer is most suitable for young people to travel in China6
Ashlynn Brooke photograph of a group2
The money of more than 100 countries and regions6
The mother was a stay-at-home children too tired took three baby suicide
Sell the barbecue as says father du breul3
Download software ranking
The cock of the Grosvenor LTD handsome
Boxer Classic video3
Tram sex maniac 2 (H) rar bag5
虚拟机汉化软件
Visual C++界面编程技术
仙剑奇侠传98硬盘WINXP版
中国结婚习俗实录
WebService在.NET中的实战应用教学视频 → 第2集
Ashlynn Video4
Ashlynn Video2
delv published in(发表于) 2014/1/10 6:28:49 Edit(编辑)
ASP.NET随机码生成示例_[Asp.Net教程]

ASP.NET随机码生成示例_[Asp.Net教程]

ASP.NET随机码生成示例_[Asp.Net教程]

现在很多网页登陆的时候都使用了随机图片的方式,是一种简单、有效的防止黑客恶意攻击的手段。今天看了一些网上的资料,明白其生成原理:从样本中,获取随机字符串,随机字符串保存进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控件的写法如下:



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


http://www.cnblogs.com/heekui/archive/2007/01/06/613609.html


来源:博客园







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.