登陆验证码

登陆验证码_第1张图片经常要用到验证码--

如何实现呢?记得前段时间在网络上看到一个方法,结合自己的程序写了一个。

1.控件image --连接到验证码程序

2.登陆程序

-----------------------------------------------------------------------------------------------------------------

验证码程序

  1. usingSystem;
  2. usingSystem.Data;
  3. usingSystem.Configuration;
  4. usingSystem.Web;
  5. usingSystem.Web.Security;
  6. usingSystem.Web.UI;
  7. usingSystem.Web.UI.WebControls;
  8. usingSystem.Web.UI.WebControls.WebParts;
  9. usingSystem.Web.UI.HtmlControls;
  10. usingSystem.Drawing;
  11. usingSystem.Drawing.Drawing2D;
  12. publicpartialclass_Default:System.Web.UI.Page
  13. {
  14. protectedvoidPage_Load(objectsender,EventArgse)
  15. {
  16. if(!IsPostBack)
  17. {
  18. Bitmapb=newBitmap(50,20);
  19. Graphicsg=Graphics.FromImage(b);
  20. g.Clear(Color.White);
  21. try
  22. {
  23. stringcode="";
  24. Randomr=newRandom();
  25. for(inti=0;i<5;i++)
  26. {
  27. code+=r.Next(0,10).ToString();
  28. }
  29. Session["Code"]=code;
  30. Fontfont=newFont("Arial",13,FontStyle.Bold);
  31. System.Drawing.Drawing2D.LinearGradientBrushbrush=newLinearGradientBrush(newRectangle(10,20,30,20),Color.Red,Color.Green,12.2f);
  32. g.DrawString(code,font,brush,0,0);
  33. System.IO.MemoryStreamstr=newSystem.IO.MemoryStream();
  34. b.Save(str,System.Drawing.Imaging.ImageFormat.Gif);
  35. Response.ClearContent();
  36. Response.ContentType="image/gif";
  37. Response.BinaryWrite(str.ToArray());
  38. }
  39. catch
  40. {
  41. g.Dispose();
  42. b.Dispose();
  43. }
  44. }
  45. }
  46. }

----------------------------------------------------------------------------------------------------------------

登陆程序

  1. usingSystem;
  2. usingSystem.Data;
  3. usingSystem.Configuration;
  4. usingSystem.Collections;
  5. usingSystem.Web;
  6. usingSystem.Web.Security;
  7. usingSystem.Web.UI;
  8. usingSystem.Web.UI.WebControls;
  9. usingSystem.Web.UI.WebControls.WebParts;
  10. usingSystem.Web.UI.HtmlControls;
  11. usingSystem.Data.SqlClient;
  12. usingSystem.Web.Caching;
  13. publicpartialclasslogin:System.Web.UI.Page
  14. {
  15. protectedvoidPage_Load(objectsender,EventArgse)
  16. {
  17. }
  18. protectedvoidButton1_Click(objectsender,EventArgse)
  19. {
  20. txtpwd.Text="";
  21. txtUserName.Text="";
  22. }
  23. protectedvoidButton2_Click(objectsender,EventArgse)
  24. {
  25. StringuserName=txtUserName.Text.ToString().Trim();
  26. StringuserPwd=txtpwd.Text.ToString().Trim();
  27. stringCode=TextBox1.Text.Trim();
  28. //StringuserPwd=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtpwd.Text,"md5");;
  29. StringStr="";
  30. Str="Selectpassword,namefromuserswherename='"+userName+"'";
  31. SqlConnectionconn=newSqlConnection(PublicVar.strCon_SHATDB);
  32. SqlCommandcmd=newSqlCommand(Str,conn);
  33. try
  34. {
  35. conn.Open();
  36. SqlDataReadersdr=cmd.ExecuteReader();
  37. if(Code==Session["Code"].ToString())
  38. {
  39. if(sdr.Read())
  40. {
  41. if(sdr.GetString(0)==userPwd)
  42. {
  43. Response.Redirect("index.aspx");
  44. }
  45. else
  46. {
  47. lblmessage.Text="密码错误,请重新输入";
  48. }
  49. }
  50. else
  51. {
  52. lblmessage.Text="该用户不存在或输入错误,请重新输入";
  53. }
  54. }
  55. else
  56. {
  57. lblmessage.Text="验证码为空或有误!";
  58. }
  59. }
  60. catch(Exceptionee)
  61. {
  62. //Response.Write("<scriptlanguage=javascript>alert('"+ee.Message.toString()+'")</script>");
  63. Response.Write("<script>alert('"+ee.Message.ToString()+"')</script>");
  64. }
  65. finally
  66. {
  67. conn.Close();
  68. }
  69. conn.Close();
  70. /*//检查用户是否已经登陆
  71. stringstrUser=string.Empty;
  72. stringstrCacheKey=txtUserName.Text.ToString().Trim();
  73. strUser=Convert.ToString(Cache[strCacheKey]);
  74. if(strUser==string.Empty)
  75. {
  76. TimeSpanSessTimeOut=newTimeSpan(0,0,System.Web.HttpContext.Current.Session.Timeout,0,0);
  77. Cache.Insert(strCacheKey,strCacheKey,null,DateTime.MaxValue,SessTimeOut,CacheItemPriority.NotRemovable,null);
  78. Session["User"]=strCacheKey;
  79. this.Label1.Text=Session["User"].ToString();
  80. }
  81. else
  82. {
  83. this.Label1.Text="这个用户已经登录!";
  84. }
  85. */
  86. }
  87. }



你可能感兴趣的:(验证码)