日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 一般处理程序ashx用于验证码

發布時間:2025/3/8 C# 62 如意码农
生活随笔 收集整理的這篇文章主要介紹了 C# 一般处理程序ashx用于验证码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、用VS2019建立一個web應用程序,選mvc模板

2、選中項目郵件新建文件夾Webservice,然后添加一般處理程序Verify.ashx然后右鍵打開改寫如下

 1  public class VerifyCode : IHttpHandler, IRequiresSessionState
2 {
3
4 public bool IsReusable { get { return false; } }
5 private const int width = 90;
6 private const int height = 30;
7 public void ProcessRequest(HttpContext context)
8 {
9 context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
10 context.Response.ClearContent();
11 context.Response.ContentType = "image/Png";
12 context.Response.BinaryWrite(CreateImg(ExfSoft.Common.SecurityUtil.CreateNew(4, true)));
13 context.Response.End();
14 }
15
16 private byte[] CreateImg(string VerifyCode)
17 {
18 //這個寬高可以根據需要確定
19 System.Drawing.Bitmap image = new Bitmap(width, height);
20 //創建圖形
21 Graphics g = Graphics.FromImage(image);
22 //創建RectangleF結構指定一個區域
23 RectangleF rectangle = new RectangleF(0, 0, image.Width, image.Height);
24 g.FillRectangle(new SolidBrush(Color.FromArgb(255, 255, 255)), rectangle);
25
26 //背景噪音線
27 System.Random random = new Random();
28 for (int i = 0; i < 15; i++)
29 {
30 int x1 = random.Next(width);
31 int x2 = random.Next(width);
32 int y1 = random.Next(height);
33 int y2 = random.Next(height);
34 g.DrawLine(new Pen(Color.FromArgb(random.Next())), x1, y1, x2, y2);
35 }
36 Font font = new Font("Arial", 14, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
37 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, width, height), Color.Blue, Color.DarkRed, 1.2f, true);
38
39 var chars = new List<Char>();
40 foreach (char s in VerifyCode)
41 {
42 chars.Add(s);
43 }
44
45 g.DrawString(String.Join(" ", chars.ToArray()), font, brush, 10, 5);
46
47 //畫圖片的前景噪音點
48 for (int i = 0; i < 50; i++)
49 {
50 int x = random.Next(width);
51 int y = random.Next(height);
52 image.SetPixel(x, y, Color.FromArgb(random.Next()));
53 }
54 //畫圖片的邊框線
55 g.DrawRectangle(new Pen(Color.FromArgb(255, 153, 193, 226)), 0, 0, image.Width - 1, image.Height - 1);
56
57 MemoryStream ms = new MemoryStream();
58 //選擇透明格式
59 image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
60 //原本是準備輸出html流,現在輸出圖信數據,所以要修改http頭
61 return ms.ToArray();
62 }
63 }

3、任意找一個頁面如Index.cshtml,添加圖片控件和Jquery代碼

<img id="verifycode_img" src="/webservice/verifycode.ashx" onclick="" style="vertical-align: middle; margin-left: 10px;" />
<script>
$(function () {
$("#verifycode_img").click(function () {
$(this).attr('src', '/WebService/VerifyCode.ashx?t=' + new Date())
})
})
</script>

4、效果,點擊圖片驗證碼將會做相應變化

5、配合input框提交到后臺Action里面通過,ExfSoft.Common.SecurityUtil.IsValid("驗證碼字符串")方法,即可以實現登錄驗證碼功能。

總結

以上是生活随笔為你收集整理的C# 一般处理程序ashx用于验证码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。