ASP.NET with C#生成验证码的过程

简介: ASP.NET with C#生成验证码的过程 生成验证码的大概过程就是在后台取得一个随机的字符串,然后该随机字符串绘制成一幅图片,当然,图片可以加上噪声,防止基本上不会出现的N人分析图形数据获取和还原字符串。

ASP.NET with C#生成验证码的过程

生成验证码的大概过程就是在后台取得一个随机的字符串,然后该随机字符串绘制成一幅图片,当然,图片可以加上噪声,防止基本上不会出现的N人分析图形数据获取和还原字符串。

具体生成验证码的代码如下,在生成随机字符串的同时会将字符串设置到一个Session["ValidateCode"] 中,实用的时候只要得到用户返回值和Session值比较就可以得出填入的验证码是否一致了。


  1 using  System;
  2 using  System.IO;
  3 using  System.Web.UI;
  4 using  System.Drawing;
  5 using  System.Drawing.Imaging;
  6 using  System.Drawing.Drawing2D;
  7
  8 namespace  Web.Common
  9 {
 10      /**/ ///  
 11      ///  validate 的摘要说明。
 12      ///  
 13      public   class  Validate: System.Web.UI.Page
 14      {
 15          private   void  Page_Load( object  sender, EventArgs e)
 16          {
 17              string  strValidateCode  =  ValidateCode( 6 ); // 取得随机字符串,并设置Session值
 18             DrawValidateCode(strValidateCode, 50 , 100 ); // 绘图
 19         }
 20     
 21          // 绘图
 22          private   void  DrawValidateCode( string  strValidateCode, int  intFgNoise, int  intBgNoise)
 23          {
 24              if (strValidateCode  ==   null   ||  strValidateCode.Trim()  ==  String.Empty)
 25              {
 26                  return ;
 27             }
 28              else
 29              {
 30                  // 建立一个位图文件 确立长宽
 31                 Bitmap bmpImage  =   new  Bitmap(( int )Math.Ceiling((strValidateCode.Length  *   12.5 )),  22 );
 32                 Graphics grpGraphics  =  Graphics.FromImage(bmpImage);
 33     
 34                  try
 35                  {
 36                      // 生成随机生成器
 37                     Random rndRandom  =   new  Random();
 38     
 39                      // 清空图片背景色
 40                     grpGraphics.Clear(Color.White);
 41     
 42                      // 画图片的背景噪音线
 43                      for ( int  i = 0 ; i < intBgNoise; i ++ )
 44                      {
 45                          int  int_x1  =  rndRandom.Next(bmpImage.Width);
 46                          int  int_x2  =  rndRandom.Next(bmpImage.Width);
 47                          int  int_y1  =  rndRandom.Next(bmpImage.Height);
 48                          int  int_y2  =  rndRandom.Next(bmpImage.Height);
 49     
 50                         grpGraphics.DrawLine( new  Pen(Color.Silver), int_x1, int_y1, int_x2, int_y2);
 51                     }
 52                      // 把产生的随机数以字体的形式写入画面
 53                     Font font  =   new  Font( " Arial " 12 , (FontStyle.Bold  |  FontStyle.Italic));
 54                     LinearGradientBrush brhBrush  =   new  LinearGradientBrush( new  Rectangle( 0 0 , bmpImage.Width, bmpImage.Height), Color.Blue, Color.DarkRed,  1.2f true );
 55                     grpGraphics.DrawString(strValidateCode, font, brhBrush,  2 2 );
 56     
 57                      // 画图片的前景噪音点
 58                      for ( int  i = 0 ; i < intFgNoise; i ++ )
 59                      {
 60                          int  int_x  =  rndRandom.Next(bmpImage.Width);
 61                          int  int_y  =  rndRandom.Next(bmpImage.Height);
 62     
 63                         bmpImage.SetPixel(int_x, int_y, Color.FromArgb(rndRandom.Next()));
 64                     }
 65     
 66                      // 画图片的边框线
 67                     grpGraphics.DrawRectangle( new  Pen(Color.Silver),  0 0 , bmpImage.Width  -   1 , bmpImage.Height  -   1 );
 68     
 69                     MemoryStream memsMemoryStream  =   new  MemoryStream();
 70                     bmpImage.Save(memsMemoryStream, ImageFormat.Gif);
 71                     Response.ClearContent();
 72                     Response.ContentType  =   " image/Gif " ;
 73                     Response.BinaryWrite(memsMemoryStream.ToArray());
 74                 }
 75                  finally
 76                  {
 77                     grpGraphics.Dispose();
 78                     bmpImage.Dispose();
 79                 }
 80             }
 81         }
 82     
 83          // 取得随机字符串,并设置Session值
 84          private   string  ValidateCode( int  intLength)
 85          {
 86              int  intNumber;
 87              char  chrCode;
 88              string  strValidateCode  =  String.Empty;
 89     
 90             Random rndRandom  =   new  Random();
 91     
 92              for ( int  i = 0 ;i < intLength;i ++ )
 93              {
 94                 intNumber  =  rndRandom.Next();
 95                  if (intNumber  %   2   ==   0 )
 96                  {
 97                     chrCode  =  ( char )( ' 0 '   +  ( char )(intNumber  %   10 )); // 如果随机数是偶数 取余
 98                 }
 99                  else
100                  {
101                     chrCode  =  ( char )( ' A '   +  ( char )(intNumber  %   26 )); // 如果随机数是奇数 选择从[A-Z]
102                 }
103                 strValidateCode  +=  chrCode.ToString(); 
104             }
105     
106             Session[ " ValidateCode " =  strValidateCode; // 设置Session["ValidateCode"]
107              // Response.Cookies.Add(new HttpCookie("strValidateCode",strValidateCode));
108     
109              return  strValidateCode;
110         }
111
112          Web 窗体设计器生成的代码 #region  Web 窗体设计器生成的代码
113          override   protected   void  OnInit(EventArgs e)
114          {
115              //
116              //  CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
117              //
118             InitializeComponent();
119              base .OnInit(e);
120         }
121         
122          /**/ ///  
123          ///  设计器支持所需的方法 - 不要使用代码编辑器修改
124          ///  此方法的内容。
125          ///  
126          private   void  InitializeComponent()
127          {    
128              this .Load  +=   new  System.EventHandler( this .Page_Load);
129
130         }
131          #endregion
132     }
133
134 }
135

目录
相关文章
|
网络协议 算法 Ubuntu
BBR一键安装脚本 BBR/魔改/暴力/BBRplus/锐速(Lotsever)
BBR是 Google 提出的一种新型拥塞控制算法,可以使 Linux 服务器显著地提高吞吐量和减少 TCP 连接的延迟
70504 4
BBR一键安装脚本 BBR/魔改/暴力/BBRplus/锐速(Lotsever)
kde
|
15天前
|
JSON Linux 数据格式
Docker镜像加速指南:手把手教你配置国内镜像源
配置国内镜像源可大幅提升 Docker 拉取速度,解决访问 Docker Hub 缓慢问题。本文详解 Linux、Docker Desktop 配置方法,并提供测速对比与常见问题解答,附最新可用镜像源列表,助力高效开发部署。
kde
9394 76
|
12天前
typora免费版,激活方法,Typora使用教程
Typora是一款简洁高效的Markdown编辑器,支持即时渲染。本教程涵盖安装方法、文件操作、视图控制、格式排版、字体样式及Markdown语法,助你快速上手使用Typora进行高效写作。
2393 6
|
5天前
|
云安全 人工智能 安全
|
18天前
|
人工智能 定位技术 API
Dify MCP 保姆级教程来了!
大语言模型,例如 DeepSeek,如果不能联网、不能操作外部工具,只能是聊天机器人。除了聊天没什么可做的。
2252 34
|
6天前
|
Ubuntu JavaScript Linux
Windows安装Claude Code
Claude Code 是 Anthropic 推出的代码助手,支持在 Windows 通过 WSL(Windows Subsystem for Linux)运行。本文介绍如何在 Windows 系统中启用 WSL、安装 Ubuntu 子系统、配置 Python 与 Node.js 环境,并最终安装和运行 Claude Code。内容涵盖 WSL 设置、开发工具安装、依赖配置及常见问题解决方法,助你顺利在本地环境中使用 Claude Code 提升编码效率。
596 1
Windows安装Claude Code
|
15天前
|
JavaScript Ubuntu IDE
国内如何安装和使用 Claude Code镜像教程 - Windows 用户篇
国内如何安装和使用 Claude Code镜像教程 - Windows 用户篇
1252 6