zoukankan      html  css  js  c++  java
  • ASP.net中的验证码实现全过程

    1.新建一个页面A.aspx,作为制作验证码的实现页   代码如下

         Protected    ovid  Page_Load(object sender,EventArgs e )

              {

                  string  path=Server.MapPath("../image/code.jpg");

                  System.Drawing.Image   image=System.Drawing.Image.FromFile( path );

                      //在图片场景中创建绘图对象Graphics

                  Graphics   g=Graphics.FromImage( image );

                  Font   font =new   Font( "宋体"  , 20 , FontStyle.Italic ) ;

                  SolidBrush  brush =new SolidBrush( Color.Blue );

                  PointF    pf = new  PointF( 2f , 2f );

                 string     str=GetString( 4 );

                 //将随即生成的验证码保存在Session中,方便在需要验证码的页面来验证

                 Session["SN"] = str ;

                 g.DrawString( str , font , brush , pf );

                    //通常我们利用Response的响应流来达到不同的数据请求目的

                 image.Save( ReSpone.OutputStream , System.Drawing.Imaging.ImageFormat.JPG ) ;

                 g.Dispose();

                 image.Dispose();

                 ReSponse.End();

             }

                      //下面方法为随机生成验证码

         Protected   void   GetString( int   num ) 

               {

                      string   str="";

                       string[]   strarray={ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "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" }; 

                      Random    r= new  Random();

                     for( int i=0 ; i<num ; i++)

                            {

                                  str+= strarray[ r.Next( strarray.Length ) ] ;

                             }

                      return   str;

               } 

    2. 在验证页面中  加一个Image 控件 和一个Html  按钮 控件( 设置其为LinkButton样式 )

              a.实现显示验证码 :

                         在Image  控件属性中 设置其  ImageUrl = "A.aspx" ; //(也就是上面的实现验证码页)

             b.实现HTMl 按钮局部刷新 验证码

                       。 设置HTMl  按钮样式   其VALue=“看不清换一张"

                       。在前台页面代码</head>前加如下代码

                                       <script  type="txtt/javascript">

                                             function   Look()

                                               {

                                                    document.GetElementById("Image1").src=' A.aspx?' +Math.random() ;

                                               }

                                       </script>

                        。在HTml  按钮中加如下 黄色部份代码

                                  <input  id=" Button1"  onclick="Look()" ……>

  • 相关阅读:
    分享关于Entity Framework 进行CRUD操作实验的结果
    总结Unity IOC容器通过配置实现类型映射的几种基本使用方法
    Python深入:Distutils发布Python模块--转载
    原创:R包制作--windows
    提高R语言速度--转载
    R 语言 Windows 环境 安装与Windows下制作R的package--Rtools
    极简 R 包建立方法--转载
    R的极客理想系列文章--转载
    如何创建R包并将其发布在 CRAN / GitHub 上--转载
    教你如何成为数据科学家(六)
  • 原文地址:https://www.cnblogs.com/yingger/p/2443277.html
Copyright © 2011-2022 走看看