ASP.NET随机码生成示例

简介: 现在很多网页登陆的时候都使用了随机图片的方式,是一种简单、有效的防止黑客恶意攻击的手段。今天看了一些网上的资料,明白其生成原理:从样本中,获取随机字符串,随机字符串保存进session,并以位图的方式形成随机码图片。

现在很多网页登陆的时候都使用了随机图片的方式,是一种简单、有效的防止黑客恶意攻击的手段。今天看了一些网上的资料,明白其生成原理:从样本中,获取随机字符串,随机字符串保存进session,并以位图的方式形成随机码图片。

实现:
添加命名空间

img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System.Drawing;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Drawing.Imaging;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Drawing.Drawing2D;

生成页代码

img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Data;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Configuration;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Collections;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Web;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Web.Security;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Web.UI;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Web.UI.WebControls;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Web.UI.WebControls.WebParts;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Web.UI.HtmlControls;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Drawing;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Drawing.Imaging;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Drawing.Drawing2D;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
public  partial  class  getRandImg : System.Web.UI.Page
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif {
img_33d02437d135341f0800e3d415312ae8.gif    
protected void Page_Load(object sender, EventArgs e)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        
//生成随机码图片
img_33d02437d135341f0800e3d415312ae8.gif
        SetValidateCode();
img_33d02437d135341f0800e3d415312ae8.gif        
//生成页面不保存到cache
img_33d02437d135341f0800e3d415312ae8.gif
        Response.Cache.SetNoStore();
img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif    
//设置验证码
img_33d02437d135341f0800e3d415312ae8.gif
    private void SetValidateCode()
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        
//新建位图
img_33d02437d135341f0800e3d415312ae8.gif
        Bitmap newBitmap = new Bitmap(
img_33d02437d135341f0800e3d415312ae8.gif                                        
71
img_33d02437d135341f0800e3d415312ae8.gif                                        
23
img_33d02437d135341f0800e3d415312ae8.gif                                        PixelFormat.Format32bppArgb
img_33d02437d135341f0800e3d415312ae8.gif                                     );
img_33d02437d135341f0800e3d415312ae8.gif        
//从位图获得绘图画面
img_33d02437d135341f0800e3d415312ae8.gif
        Graphics g = Graphics.FromImage(newBitmap);
img_33d02437d135341f0800e3d415312ae8.gif        
//随机数生成器
img_33d02437d135341f0800e3d415312ae8.gif
        Random r = new Random();
img_33d02437d135341f0800e3d415312ae8.gif        
//绘图画面清空
img_33d02437d135341f0800e3d415312ae8.gif
        g.Clear(Color.White);
img_33d02437d135341f0800e3d415312ae8.gif        
//绘图画面划线干扰
img_33d02437d135341f0800e3d415312ae8.gif
        for (int i = 0; i < 50; i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            
int x1 = r.Next(newBitmap.Width);
img_33d02437d135341f0800e3d415312ae8.gif            
int x2 = r.Next(newBitmap.Width);
img_33d02437d135341f0800e3d415312ae8.gif            
int y1 = r.Next(newBitmap.Height);
img_33d02437d135341f0800e3d415312ae8.gif            
int y2 = r.Next(newBitmap.Height);
img_33d02437d135341f0800e3d415312ae8.gif            g.DrawLine(
new Pen(
img_33d02437d135341f0800e3d415312ae8.gif                                Color.FromArgb(r.Next())), 
img_33d02437d135341f0800e3d415312ae8.gif                                x1, 
img_33d02437d135341f0800e3d415312ae8.gif                                y1, 
img_33d02437d135341f0800e3d415312ae8.gif                                x2, 
img_33d02437d135341f0800e3d415312ae8.gif                                y2
img_33d02437d135341f0800e3d415312ae8.gif                              );
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif        
//绘图画面点数干扰
img_33d02437d135341f0800e3d415312ae8.gif
        for (int i = 0; i < 100; i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            
int x = r.Next(newBitmap.Width);
img_33d02437d135341f0800e3d415312ae8.gif            
int y = r.Next(newBitmap.Height);
img_33d02437d135341f0800e3d415312ae8.gif            newBitmap.SetPixel(
img_33d02437d135341f0800e3d415312ae8.gif                                x, 
img_33d02437d135341f0800e3d415312ae8.gif                                y, 
img_33d02437d135341f0800e3d415312ae8.gif                                Color.FromArgb(r.Next())
img_33d02437d135341f0800e3d415312ae8.gif                              );
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif        
//获得随机字符串(5位长度)
img_33d02437d135341f0800e3d415312ae8.gif
        string value = GenerateRandom(5);
img_33d02437d135341f0800e3d415312ae8.gif        
//随机字符串赋值给Session
img_33d02437d135341f0800e3d415312ae8.gif
        Session["RandCode"= value;
img_33d02437d135341f0800e3d415312ae8.gif        
//定义图片显示字体样式
img_33d02437d135341f0800e3d415312ae8.gif
        Font font = new Font(
img_33d02437d135341f0800e3d415312ae8.gif                               
"Arial"
img_33d02437d135341f0800e3d415312ae8.gif                               
14
img_33d02437d135341f0800e3d415312ae8.gif                               FontStyle.Bold
img_33d02437d135341f0800e3d415312ae8.gif                            );
img_33d02437d135341f0800e3d415312ae8.gif        Random rr 
= new Random();
img_33d02437d135341f0800e3d415312ae8.gif        
int yy = rr.Next(14);
img_33d02437d135341f0800e3d415312ae8.gif        
//定义随机字符串显示图片刷子
img_33d02437d135341f0800e3d415312ae8.gif
        LinearGradientBrush brush = new LinearGradientBrush(
img_33d02437d135341f0800e3d415312ae8.gif                                                              
new Rectangle(007123), 
img_33d02437d135341f0800e3d415312ae8.gif                                                              Color.Red, 
img_33d02437d135341f0800e3d415312ae8.gif                                                              Color.Blue, 
img_33d02437d135341f0800e3d415312ae8.gif                                                              
1.2f
img_33d02437d135341f0800e3d415312ae8.gif                                                              
true
img_33d02437d135341f0800e3d415312ae8.gif                                                           );
img_33d02437d135341f0800e3d415312ae8.gif        g.DrawString(value, font, brush, 
2, yy);
img_33d02437d135341f0800e3d415312ae8.gif        g.DrawRectangle(
new Pen(
img_33d02437d135341f0800e3d415312ae8.gif                                  Color.Silver), 
img_33d02437d135341f0800e3d415312ae8.gif                                  
0
img_33d02437d135341f0800e3d415312ae8.gif                                  
0
img_33d02437d135341f0800e3d415312ae8.gif                                  
70
img_33d02437d135341f0800e3d415312ae8.gif                                  
22
img_33d02437d135341f0800e3d415312ae8.gif                                );
img_33d02437d135341f0800e3d415312ae8.gif        System.IO.MemoryStream ms 
= new System.IO.MemoryStream();
img_33d02437d135341f0800e3d415312ae8.gif        newBitmap.Save(ms, ImageFormat.Gif);
img_33d02437d135341f0800e3d415312ae8.gif        
//输出图片
img_33d02437d135341f0800e3d415312ae8.gif
        Response.ClearContent();
img_33d02437d135341f0800e3d415312ae8.gif        Response.ContentType 
= "image/gif";
img_33d02437d135341f0800e3d415312ae8.gif        Response.BinaryWrite(ms.ToArray());
img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif    
//常量集
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif
    private static char[] constant =img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                                        
'0','1','2','3','4','5','6','7','8','9',
img_33d02437d135341f0800e3d415312ae8.gif                                        
'a','b','c','d','e','f','g','h','i','j',
img_33d02437d135341f0800e3d415312ae8.gif                                        
'k','l','m','n','o','p','q','r','s','t',
img_33d02437d135341f0800e3d415312ae8.gif                                        
'u','v','w','x','y','z'
img_105a1e124122b2abcee4ea8e9f5108f3.gif                                    }
;
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif    
//生成随机字符串
img_33d02437d135341f0800e3d415312ae8.gif
    public static string GenerateRandom(int Length)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        System.Text.StringBuilder newRandom 
= new System.Text.StringBuilder(36);
img_33d02437d135341f0800e3d415312ae8.gif        Random rd 
= new Random();
img_33d02437d135341f0800e3d415312ae8.gif        
for (int i = 0; i < Length; i++)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            newRandom.Append(constant[rd.Next(
36)]);
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif        
return newRandom.ToString();
img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_05dd8d549cff04457a6366b0a7c9352a.gif}

img_a6339ee3e57d1d52bc7d02b338e15a60.gif

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

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

示例代码:/Files/heekui/RandCode.rar

目录
相关文章
|
存储 开发框架 前端开发
asp.net与asp.net优缺点及示例
asp.net与asp.net优缺点及示例
109 0
|
4月前
|
API
【Azure 媒体服务】Media Service的编码示例 -- 创建缩略图子画面的.NET代码调试问题
【Azure 媒体服务】Media Service的编码示例 -- 创建缩略图子画面的.NET代码调试问题
|
4月前
|
XML API 图形学
【Azure Developer】.Net 简单示例 "文字动图显示" Typing to SVG
【Azure Developer】.Net 简单示例 "文字动图显示" Typing to SVG
【Azure Developer】.Net 简单示例 "文字动图显示" Typing to SVG
|
6月前
|
XML API 图形学
.Net 简单示例 "文字动图显示" Typing to SVG “
该文描述了一个.NET API的实现过程,该API能将输入的文字转换成SVG动态图。首先,作者展示了示例网站(&lt;https://readme-typing-svg.demolab.com/&gt;)的功能,它能将文字转化为可自定义样式的SVG动画。接着分析了示例URL的响应,发现其内容类型为`image/svg+xml`,主要由SVG、path、animate和text元素组成。通过创建一个.NET Core Web API项目,作者设置了响应内容类型为`image/svg+xml`,并将示例URL的SVG内容直接输出,成功实现了相同效果。
|
7月前
|
前端开发 数据库连接 数据库
ASP.NETMVC数据库完整CRUD操作示例
ASP.NETMVC数据库完整CRUD操作示例
72 0
|
7月前
|
SQL 开发框架 .NET
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
88 0
|
开发工具 索引
NET SDK连接阿里云ElasticSearch示例
本文主要演示如何使用elasticsearch-net sdk连接阿里云ElasticSearch实例,通过index的创建进行验证测试。
479 0
NET SDK连接阿里云ElasticSearch示例
|
自然语言处理 API 开发工具
阿里云自然语言处理 .NET 使用示例
本文主要针对目前官方文档中参考Demo使用SDK较老且本身中文打印输出异常的问题,提供包括SDK安装步骤+正常可用示例.NET Demo。
阿里云自然语言处理 .NET 使用示例
【STM32 .Net MF开发板学习-03】TinyGUI绘图示例
.Net Micro Framework官方图形库是WPF,由于目前ST Cortex-M3开发板RAM太小,最大才512K(常见是128K或256k),并且Cortex-M3的CPU主频也不太高,运行WPF图形框架显得过于重了,所以我这边推出了轻量级图形库TinyGUI
611 0