VerificationCodeModule.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Drawing;
  6. using System.Web.UI.WebControls;
  7. using System.Drawing.Imaging;
  8. using System.Web.SessionState;
  9. namespace Bowin.Common
  10. {
  11. public class VerificationCodeModule : IHttpModule, IRequiresSessionState
  12. {
  13. public void Dispose()
  14. { }
  15. public void Init(HttpApplication context)
  16. {
  17. context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
  18. }
  19. void context_PreRequestHandlerExecute(object sender, EventArgs e)
  20. {
  21. HttpApplication context = (HttpApplication)sender;
  22. string str = context.Request.Url.ToString();
  23. if (str.Contains("verificationCode.png"))
  24. {
  25. //context.Response.Cache.SetNoStore();
  26. this._random = new Random();
  27. this._code = GetRandomCode();
  28. context.Session["code"] = this._code;
  29. this.SetPageNoCache(context);
  30. this.OnPaint(context);
  31. context.Response.End();
  32. }
  33. else
  34. { return; }
  35. }
  36. static string[] FontItems = new string[]
  37. {
  38. "Arial",
  39. "Helvetica",
  40. "Geneva",
  41. "sans-serif",
  42. "Verdana"
  43. };
  44. static Brush[] BrushItems = new Brush[]
  45. {
  46. Brushes.OliveDrab,
  47. Brushes.ForestGreen,
  48. Brushes.DarkCyan,
  49. Brushes.LightSlateGray,
  50. Brushes.RoyalBlue,
  51. Brushes.SlateBlue,
  52. Brushes.DarkViolet,
  53. Brushes.MediumVioletRed,
  54. Brushes.IndianRed,
  55. Brushes.Firebrick,
  56. Brushes.Chocolate,
  57. Brushes.Peru,
  58. Brushes.Goldenrod
  59. };
  60. static string[] BrushName = new string[] {
  61. "OliveDrab",
  62. "ForestGreen",
  63. "DarkCyan",
  64. "LightSlateGray",
  65. "RoyalBlue",
  66. "SlateBlue",
  67. "DarkViolet",
  68. "MediumVioletRed",
  69. "IndianRed",
  70. "Firebrick",
  71. "Chocolate",
  72. "Peru",
  73. "Goldenrod"
  74. };
  75. private static Color BackColor = Color.White;
  76. private static Pen BorderColor = Pens.DarkGray;
  77. private static int Width = 52;
  78. private static int Height = 21;
  79. private Random _random;
  80. private string _code;
  81. private int _brushNameIndex;
  82. /**/
  83. /**/
  84. /**/
  85. /// <summary>
  86. /// Required method for Designer support - do not modify
  87. /// the contents of this method with the code editor.
  88. /// </summary>
  89. private void InitializeComponent()
  90. {
  91. //this.Load += new System.EventHandler(this.Page_Load);
  92. }
  93. /**/
  94. /**/
  95. /**/
  96. /// <summary>
  97. /// 设置页面不被缓存
  98. /// </summary>
  99. private void SetPageNoCache(HttpApplication context)
  100. {
  101. context.Response.Buffer = true;
  102. context.Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
  103. context.Response.Expires = 0;
  104. context.Response.CacheControl = "no-cache";
  105. context.Response.AppendHeader("Pragma", "No-Cache");
  106. }
  107. /**/
  108. /**/
  109. /**/
  110. /// <summary>
  111. /// 取得一个 4 位的随机码
  112. /// </summary>
  113. /// <returns></returns>
  114. private string GetRandomCode()
  115. {
  116. return Guid.NewGuid().ToString().Substring(0, 4);
  117. }
  118. /**/
  119. /**/
  120. /**/
  121. /// <summary>
  122. /// 随机取一个字体
  123. /// </summary>
  124. /// <returns></returns>
  125. private Font GetFont()
  126. {
  127. int fontIndex = _random.Next(0, FontItems.Length);
  128. FontStyle fontStyle = GetFontStyle(_random.Next(0, 2));
  129. return new Font(FontItems[fontIndex], 12, fontStyle);
  130. }
  131. /**/
  132. /**/
  133. /**/
  134. /// <summary>
  135. /// 取一个字体的样式
  136. /// </summary>
  137. /// <param name="index"></param>
  138. /// <returns></returns>
  139. private FontStyle GetFontStyle(int index)
  140. {
  141. switch (index)
  142. {
  143. case 0:
  144. return FontStyle.Bold;
  145. case 1:
  146. return FontStyle.Italic;
  147. default:
  148. return FontStyle.Regular;
  149. }
  150. }
  151. /**/
  152. /**/
  153. /**/
  154. /// <summary>
  155. /// 随机取一个笔刷
  156. /// </summary>
  157. /// <returns></returns>
  158. private Brush GetBrush()
  159. {
  160. int brushIndex = _random.Next(0, BrushItems.Length);
  161. _brushNameIndex = brushIndex;
  162. return BrushItems[brushIndex];
  163. }
  164. /**/
  165. /**/
  166. /**/
  167. /// <summary>
  168. /// 绘画事件
  169. /// </summary>
  170. private void OnPaint(HttpApplication context)
  171. {
  172. Bitmap objBitmap = null;
  173. Graphics g = null;
  174. try
  175. {
  176. objBitmap = new Bitmap(Width, Height);
  177. g = Graphics.FromImage(objBitmap);
  178. Paint_Background(g);
  179. Paint_Text(g);
  180. Paint_TextStain(objBitmap);
  181. Paint_Border(g);
  182. objBitmap.Save(context.Response.OutputStream, ImageFormat.Gif);
  183. context.Response.ContentType = "image/gif";
  184. }
  185. catch { }
  186. finally
  187. {
  188. if (null != objBitmap)
  189. objBitmap.Dispose();
  190. if (null != g)
  191. g.Dispose();
  192. }
  193. }
  194. /**/
  195. /**/
  196. /**/
  197. /// <summary>
  198. /// 绘画背景颜色
  199. /// </summary>
  200. /// <param name="g"></param>
  201. private void Paint_Background(Graphics g)
  202. {
  203. g.Clear(BackColor);
  204. }
  205. /**/
  206. /**/
  207. /**/
  208. /// <summary>
  209. /// 绘画边框
  210. /// </summary>
  211. /// <param name="g"></param>
  212. private void Paint_Border(Graphics g)
  213. {
  214. g.DrawRectangle(BorderColor, 0, 0, Width - 1, Height - 1);
  215. }
  216. /**/
  217. /**/
  218. /**/
  219. /// <summary>
  220. /// 绘画文字
  221. /// </summary>
  222. /// <param name="g"></param>
  223. private void Paint_Text(Graphics g)
  224. {
  225. g.DrawString(_code, GetFont(), GetBrush(), 3, 1);
  226. }
  227. /**/
  228. /**/
  229. /**/
  230. /// <summary>
  231. /// 绘画文字噪音点
  232. /// </summary>
  233. /// <param name="g"></param>
  234. private void Paint_TextStain(Bitmap b)
  235. {
  236. for (int n = 0; n < 30; n++)
  237. {
  238. int x = _random.Next(Width);
  239. int y = _random.Next(Height);
  240. b.SetPixel(x, y, Color.FromName(BrushName[_brushNameIndex]));
  241. }
  242. }
  243. }
  244. }