验证码的生成与使用

来源:互联网 发布:unity麻将源码 编辑:程序博客网 时间:2024/05/29 15:19

验证码的生成

 

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace bargaining
{
 /// <summary>
 /// images 的摘要说明。
 /// </summary>
 public class images : System.Web.UI.Page
 {
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   this.drawing();
  }
  private string RadomNum(int n)
  {
   string yzm="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,u,v,w,x,y,z,t,r,s";
   string[] vcarry=yzm.Split(',');
   string vnum="";
   int temp=-1;
   System.Random rand=new Random();
   for(int i=1;i<=n;i++)
   {
    if(temp!=-1)
    {
     rand=new Random(i*temp*(int)System.DateTime.Now.Ticks);
    }
    int t=rand.Next(35);
    if(temp!=-1 && temp==t)
    {
     return this.RadomNum(n);
    }
    temp=t;
    vnum+=vcarry[t];
   }
   return vnum;
  }
  private void drawing()
  {
   System.Drawing.Bitmap objbitmap=new Bitmap(120,60);
   
   string str=this.RadomNum(4);
   this.Session["yzm"]=str;
   System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(objbitmap);
   g.Clear(System.Drawing.Color.Snow);
   g.DrawString(str,new Font("Arial", 30, System.Drawing.FontStyle.Bold),new SolidBrush(System.Drawing.Color.Black),10,8);

   System.IO.MemoryStream ms = new System.IO.MemoryStream();
   objbitmap.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
   Response.ClearContent();
   Response.ContentType = "image/Gif";
   Response.BinaryWrite(ms.ToArray());
   g.Dispose();
   objbitmap.Dispose();
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

 

验证码的使用(在用户控件里使用)

 

namespace bargaining.ascx
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;
 using System.Data.SqlClient;

 /// <summary>
 ///  frmlogin 的摘要说明。
 /// </summary>
 public class frmlogin : System.Web.UI.UserControl
 {
  protected System.Web.UI.WebControls.TextBox txtName;
  protected System.Web.UI.WebControls.TextBox txtPassword;
  protected System.Web.UI.HtmlControls.HtmlTable TABLE2;
  protected System.Web.UI.HtmlControls.HtmlImage IMG1;
  protected System.Web.UI.WebControls.LinkButton LinkButton1;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.LinkButton LinkButton2;
  protected System.Web.UI.HtmlControls.HtmlTable TABLE1;
  protected System.Web.UI.WebControls.ImageButton ImageButton1;
  protected System.Web.UI.WebControls.LinkButton LinkButton3;
  protected System.Web.UI.WebControls.ImageButton ImageButton2;
  protected System.Web.UI.WebControls.LinkButton LinkButton4;
  protected System.Web.UI.WebControls.LinkButton LinkButton5;
  protected System.Web.UI.WebControls.LinkButton LinkButton6;
  protected System.Web.UI.WebControls.TextBox Text2;
  protected System.Web.UI.WebControls.Label Label4;

  private void Page_Load(object sender, System.EventArgs e)
  {
   if(this.Session["UserID"]!=null)
   {
    this.Label1.Text=this.Session["UserName"].ToString();
    DataSet account=null;
    using(WebShop.Common common=new WebShop.Common())
    {
     account=common.full("pro_selectaccountbyuserid",CommandType.StoredProcedure,"accunt",new SqlParameter("@userid",int.Parse(this.Session["UserID"].ToString())));
     this.Session["account"]=account;
     if(account.Tables[0].Rows.Count>0)
     {
      float zhong=float.Parse(account.Tables[0].Rows[0][1].ToString());
      float keyong=float.Parse(account.Tables[0].Rows[0][2].ToString());
      float dongjie=float.Parse(account.Tables[0].Rows[0][3].ToString());
      
      
      this.Label2.Text=zhong.ToString("F2");
      this.Label3.Text=keyong.ToString("F2");
      this.Label4.Text=dongjie.ToString("F2");
     }
    }
    this.TABLE1.Visible=true;
    
    this.TABLE2.Visible=false;
    
   }
   else
   {
    this.TABLE1.Visible=false;
    this.TABLE2.Visible=true;
   }
   if(!this.IsPostBack)
   {
    this.IMG1.Src="../images.aspx";
   }
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  ///  设计器支持所需的方法 - 不要使用代码编辑器
  ///  修改此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
   this.ImageButton2.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton2_Click);
   this.LinkButton6.Click += new System.EventHandler(this.LinkButton6_Click);
   this.LinkButton4.Click += new System.EventHandler(this.LinkButton4_Click);
   this.LinkButton5.Click += new System.EventHandler(this.LinkButton5_Click);
   this.LinkButton3.Click += new System.EventHandler(this.LinkButton3_Click);
   this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

//  private string RadomNum(int n)
//  {
//   string yzm="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,u,v,w,x,y,z,t,r,s";
//   string[] vcarry=yzm.Split(',');
//   string vnum="";
//   int temp=-1;
//   System.Random rand=new Random();
//   for(int i=1;i<=n;i++)
//   {
//    if(temp!=-1)
//    {
//     rand=new Random(i*temp*(int)System.DateTime.Now.Ticks);
//    }
//    int t=rand.Next(35);
//    if(temp!=-1 && temp==t)
//    {
//     return this.RadomNum(n);
//    }
//    temp=t;
//    vnum+=vcarry[t];
//   }
//   return vnum;
//  }
//  private void drawing()
//  {
//   System.Drawing.Bitmap objbitmap=new Bitmap(180,80);
   
//   System.Random objradom=new Random();
//   for(int i=1;i<=1000;i++)
//   {
//    objbitmap.SetPixel(objradom.Next(200),objradom.Next(80),System.Drawing.Color.Black);
//   }

//   string str=this.RadomNum(4);
//   this.Session["yzm"]=str;
//   System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(objbitmap);
//   g.Clear(System.Drawing.Color.Pink);
//   g.DrawString(str,new Font("Arial", 40, System.Drawing.FontStyle.Bold),new SolidBrush(System.Drawing.Color.Black),20,8);

//   Pen blackPen = new Pen(Color.Black, 0);
//   Random rand = new Random();
//   for (int i=0;i<5;i++)
//   {
//    int y = rand.Next(objbitmap.Height);
//    g.DrawLine(blackPen,0,y,objbitmap.Width,y);
//   }

//   string fullname=this.Server.MapPath("images")+"//yz.gif";
//   objbitmap.Save(fullname,System.Drawing.Imaging.ImageFormat.Gif);
//   this.IMG1.Src="../images/yz.gif";

//  }
  private void LinkButton1_Click(object sender, System.EventArgs e)
  {
   
   System.Web.Security.FormsAuthentication.SignOut();
   this.Session.Abandon();
   this.TABLE1.Visible=false;
   this.TABLE2.Visible=true;
  }

  private void LinkButton2_Click(object sender, System.EventArgs e)
  {
   this.IMG1.Src="../images.aspx";
  }

  private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   if(this.Session["yzm"].ToString()!=this.Text2.Text)
    {
     this.Response.Write("<script language='javascript'>alert('验证码错误')</script>");
     this.IMG1.Src="../images.aspx";
     return;
    }
    if(this.txtName.Text=="")
    {
     this.Response.Write("<script language='javascript'>alert('请输入用户名')</script>");
    }
    if(this.txtPassword.Text=="")
    {
     this.Response.Write("<script language='javascript'>alert('请输入密码')</script>");
    }
    using(WebShop.Common common=new WebShop.Common())
    {
     DataSet ds=null;
     ds=common.full("pro_selectusersbyusername",CommandType.StoredProcedure,"users",new SqlParameter("@username",this.txtName.Text.Trim()),new SqlParameter("@passwords",this.txtPassword.Text.Trim()));
      
     if(ds.Tables[0].Rows.Count>0)
     {
      if(ds.Tables[0].Rows[0][6].ToString()!="冻结")
      {
       int userid=int.Parse(ds.Tables["users"].Rows[0][0].ToString());
       this.Session["UserID"]=userid;
       this.Session["UserName"]=ds.Tables["users"].Rows[0][1].ToString();
       this.Session["Email"]=ds.Tables["users"].Rows[0][4].ToString();

       this.Application.Lock();
//       string aaa=this.Application["userid"].ToString();
       if(this.Application["userid"].ToString().Trim()=="")
       {
        this.Application["userid"]=userid.ToString();
       }
       else
       {
        this.Application["userid"]=this.Application["userid"].ToString().Trim()+","+userid.ToString();
       }
//       aaa=this.Application["userid"].ToString();
       this.Application.UnLock();


       System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.txtName.Text,false);
       Response.Redirect("index.aspx");
       
      }
      else
      {
       this.Response.Write("<script> alert('您的账户已经被冻结!')</script>");
       this.IMG1.Src="../images.aspx";
       return;
      }
     }
     else
     {
      this.Response.Write("<script> alert('用户名或密码错误!')</script>");
      this.IMG1.Src="../images.aspx";
      return;
     }
    }
  }

  private void ImageButton2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   this.Response.Redirect("UserRegisterinfo.aspx");
  }

  private void LinkButton3_Click(object sender, System.EventArgs e)
  {
   Response.Redirect("gerenshangdian.aspx");
  }

  private void LinkButton4_Click(object sender, System.EventArgs e)
  {
   Response.Redirect("updateuser.aspx");
  }

  private void LinkButton5_Click(object sender, System.EventArgs e)
  {
   Response.Redirect("SoCang.aspx");
  }

  private void LinkButton6_Click(object sender, System.EventArgs e)
  {
   Response.Redirect("updatepassword.aspx");
  }


 }
}

 

 

原创粉丝点击