经常要用到验证码--
如何实现呢?记得前段时间在网络上看到一个方法,结合自己的程序写了一个。
1.控件image --连接到验证码程序
2.登陆程序
-----------------------------------------------------------------------------------------------------------------
验证码程序
- usingSystem;
- usingSystem.Data;
- usingSystem.Configuration;
- usingSystem.Web;
- usingSystem.Web.Security;
- usingSystem.Web.UI;
- usingSystem.Web.UI.WebControls;
- usingSystem.Web.UI.WebControls.WebParts;
- usingSystem.Web.UI.HtmlControls;
- usingSystem.Drawing;
- usingSystem.Drawing.Drawing2D;
- publicpartialclass_Default:System.Web.UI.Page
- {
- protectedvoidPage_Load(objectsender,EventArgse)
- {
- if(!IsPostBack)
- {
- Bitmapb=newBitmap(50,20);
- Graphicsg=Graphics.FromImage(b);
- g.Clear(Color.White);
- try
- {
- stringcode="";
- Randomr=newRandom();
- for(inti=0;i<5;i++)
- {
- code+=r.Next(0,10).ToString();
- }
- Session["Code"]=code;
- Fontfont=newFont("Arial",13,FontStyle.Bold);
- System.Drawing.Drawing2D.LinearGradientBrushbrush=newLinearGradientBrush(newRectangle(10,20,30,20),Color.Red,Color.Green,12.2f);
- g.DrawString(code,font,brush,0,0);
- System.IO.MemoryStreamstr=newSystem.IO.MemoryStream();
- b.Save(str,System.Drawing.Imaging.ImageFormat.Gif);
- Response.ClearContent();
- Response.ContentType="image/gif";
- Response.BinaryWrite(str.ToArray());
- }
- catch
- {
- g.Dispose();
- b.Dispose();
- }
- }
- }
- }
----------------------------------------------------------------------------------------------------------------
登陆程序
- usingSystem;
- usingSystem.Data;
- usingSystem.Configuration;
- usingSystem.Collections;
- usingSystem.Web;
- usingSystem.Web.Security;
- usingSystem.Web.UI;
- usingSystem.Web.UI.WebControls;
- usingSystem.Web.UI.WebControls.WebParts;
- usingSystem.Web.UI.HtmlControls;
- usingSystem.Data.SqlClient;
- usingSystem.Web.Caching;
- publicpartialclasslogin:System.Web.UI.Page
- {
- protectedvoidPage_Load(objectsender,EventArgse)
- {
- }
- protectedvoidButton1_Click(objectsender,EventArgse)
- {
- txtpwd.Text="";
- txtUserName.Text="";
- }
- protectedvoidButton2_Click(objectsender,EventArgse)
- {
- StringuserName=txtUserName.Text.ToString().Trim();
- StringuserPwd=txtpwd.Text.ToString().Trim();
- stringCode=TextBox1.Text.Trim();
- //StringuserPwd=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtpwd.Text,"md5");;
- StringStr="";
- Str="Selectpassword,namefromuserswherename='"+userName+"'";
- SqlConnectionconn=newSqlConnection(PublicVar.strCon_SHATDB);
- SqlCommandcmd=newSqlCommand(Str,conn);
- try
- {
- conn.Open();
- SqlDataReadersdr=cmd.ExecuteReader();
- if(Code==Session["Code"].ToString())
- {
- if(sdr.Read())
- {
- if(sdr.GetString(0)==userPwd)
- {
- Response.Redirect("index.aspx");
- }
- else
- {
- lblmessage.Text="密码错误,请重新输入";
- }
- }
- else
- {
- lblmessage.Text="该用户不存在或输入错误,请重新输入";
- }
- }
- else
- {
- lblmessage.Text="验证码为空或有误!";
- }
- }
- catch(Exceptionee)
- {
- //Response.Write("<scriptlanguage=javascript>alert('"+ee.Message.toString()+'")</script>");
- Response.Write("<script>alert('"+ee.Message.ToString()+"')</script>");
- }
- finally
- {
- conn.Close();
- }
- conn.Close();
- /*//检查用户是否已经登陆
- stringstrUser=string.Empty;
- stringstrCacheKey=txtUserName.Text.ToString().Trim();
- strUser=Convert.ToString(Cache[strCacheKey]);
- if(strUser==string.Empty)
- {
- TimeSpanSessTimeOut=newTimeSpan(0,0,System.Web.HttpContext.Current.Session.Timeout,0,0);
- Cache.Insert(strCacheKey,strCacheKey,null,DateTime.MaxValue,SessTimeOut,CacheItemPriority.NotRemovable,null);
- Session["User"]=strCacheKey;
- this.Label1.Text=Session["User"].ToString();
- }
- else
- {
- this.Label1.Text="这个用户已经登录!";
- }
- */
- }
- }