Asp.net write login verification code

Source: Internet
Author: User

Login. aspx. cs File

  1. Using System. Drawing;
  2. Using System. Drawing. Imaging;
  3. Private void Page_Load (object sender, System. EventArgs e)
  4. {
  5. // Place user code here to initialize the page
  6. // RndNum is a user-defined function.
  7. String VNum = RndNum (4); // here the number 4 represents a 4-digit verification string!
  8. Session ["VNum"] = VNum;
  9. ValidateCode (VNum );
  10. }
  11. // Image generation function
  12. Private void ValidateCode (string VNum)
  13. {
  14. Int Gheight = (int) (VNum. Length * 11.5 );
  15. // Gheight indicates the image width. The image width is automatically changed based on the Character length.
  16. System. Drawing. Bitmap Img = new System. Drawing. Bitmap (Gheight, 20 );
  17. Graphics g = Graphics. FromImage (Img );
  18. G. DrawString (VNum, new System. Drawing. Font ("Arial", 10), new System. Drawing. SolidBrush (Color. Red), 3,3 );
  19. // Draw a string in the rectangle (string, Font, paint color, top left x. Top left y)
  20. System. IO. MemoryStream MS = new System. IO. MemoryStream ();
  21. Img. Save (MS, System. Drawing. Imaging. ImageFormat. Png );
  22. Response. ClearContent (); // You Need to output the image information to modify the HTTP header.
  23. Response. ContentType = "image/Png ";
  24. Response. BinaryWrite (ms. ToArray ());
  25. G. Dispose ();
  26. Img. Dispose ();
  27. Response. End ();
  28. }
  29. // The random number generation function is randomly extracted from the Vchar array.
  30. // Uppercase/lowercase letters
  31. Public string RndNum (int VcodeNum)
  32. {
  33. String Vchar = ", A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T, U, W, X, Y, Z ";
  34. String [] VcArray = Vchar. Split (,);
  35. String VNum = ""; // StringBuilder is not required because the string is short.
  36. Int temp =-1; // record the previous random value. Avoid producing several identical random numbers.
  37. // Use a simple algorithm to ensure different random numbers are generated
  38. Random rand = new Random ();
  39. For (int I = 1; I <VcodeNum 1; I)
  40. {
  41. If (temp! =-1)
  42. {
  43. Rand = new Random (I * temp * unchecked (int) DateTime. Now. Ticks ));
  44. }
  45. // Int t = rand. Next (35 );
  46. Int t = rand. Next (35 );
  47. If (temp! =-1 & temp = t)
  48. {
  49. Return RndNum (VcodeNum );
  50. }
  51. Temp = t;
  52. VNum = VcArray [t];
  53. }
  54. Return VNum;
  55. }
  56. Public void doit (object sender, System. EventArgs e)
  57. {
  58. If (Page. IsValid)
  59. {
  60. String VNum;
  61. VNum = Session ["VNum"]. ToString ();
  62. ViewState ["VNum"] = VNum;
  63. If (this. Vcode. Text = ViewState ["VNum"]. ToString ())
  64. {
  65. Hover. Manage. CheckLogin obj = new Hover. Manage. CheckLogin ();
  66. String name = username. Text;
  67. String password = FormsAuthentication. HashPasswordForStoringInConfigFile (pass. Text. ToString (), "md5 ");
  68. If (! Obj. checklogin (name, password ))
  69. {
  70. Response. Redirect (".../Error. aspx? Action = Errorlogin ");
  71. Response. End ();
  72. Return;
  73. }
  74. Else
  75. {
  76. Session. Add ("adminname", name );
  77. Session. Add ("adminpass", password );
  78. Response. Redirect ("Default. aspx ");
  79. Response. End ();
  80. Return;
  81. }
  82. }
  83. Else
  84. {
  85. Response. Write ("<script> alert (" enter the correct additional code! "); </Script> ");
  86. }
  87. }
  88. }
  89. // Verify if the submitted verification string is correct!
  90. // Otherwise, the system prompts you to return to the logon page! You can also simplify this page!
  91. If (this. Vcode. Text = ViewState ["VNum"]. ToString ())
  92. {
  93. Response. Write ("correct Verification Code ");
  94. }
  95. Else
  96. {
  97. Response. Write ("Verification code error! ");
  98. }
  99. The following is the response event of Login. Aspx in which the submit button is called.
  100. Public void doit (object sender, System. EventArgs e)
  101. {
  102. If (Page. IsValid)
  103. {
  104. String VNum;
  105. VNum = Session ["VNum"]. ToString ();
  106. ViewState ["VNum"] = VNum;
  107. If (this. Vcode. Text = ViewState ["VNum"]. ToString ())
  108. {
  109. Hover. Manage. CheckLogin obj = new Hover. Manage. CheckLogin ();
  110. String name = username. Text;
  111. String password = FormsAuthentication. HashPasswordForStoringInConfigFile (pass. Text. ToString (), "md5 ");
  112. If (! Obj. checklogin (name, password ))
  113. {
  114. Response. Redirect (".../Error. aspx? Action = Errorlogin ");
  115. Response. End ();
  116. Return;
  117. }
  118. Else
  119. {
  120. Session. Add ("adminname", name );
  121. Session. Add ("adminpass", password );
  122. Response. Redirect ("Default. aspx ");
  123. Response. End ();
  124. Return;
  125. }
  126. }
  127. Else
  128. {
  129. Response. Write ("<script> alert (" enter the correct additional code! "); </Script> ");
  130. }
  131. }
  132. }
  133. If the verification string submitted is correct, verify that it is a legal user!
  134. Otherwise, the system prompts you to return to the logon page! You can also simplify this page!
  135. If (this. Vcode. Text = ViewState ["VNum"]. ToString ())
  136. {
  137. Response. Write ("correct Verification Code ");
  138. }
  139. Else
  140. {
  141. Response. Write ("Verification code error! ");
  142. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.