ashx-auth-黑色简洁验证码

本文介绍了一种黑色简洁验证码的实现方法,使用C#语言通过绘制随机字符并添加背景干扰线来生成验证码图片,并通过WebHandler处理用户反馈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ylbtech-util: ashx-auth-黑色简洁验证码

 ashx-auth-黑色简洁验证码

1.A,效果图返回顶部
 
1.B,源代码返回顶部

/ImageUniqueCode.ashx

<%@ WebHandler Language="C#" Class="ImageUniqueCode" %>

using System;
using System.Web;
using System.Drawing;
using System.Text;

public class ImageUniqueCode : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/gif";
        //建立Bitmap对象,绘图
        Bitmap basemap = new Bitmap(160, 60);
        Graphics graph = Graphics.FromImage(basemap);
        graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 160, 60);
        Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
        Random r = new Random();
        string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";
        string letter;
        StringBuilder s = new StringBuilder();

        //添加随机字符
        for (int x = 0; x < 4; x++)
        {
            letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
            s.Append(letter);
            graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));
        }

        //混淆背景
        Pen linePen = new Pen(new SolidBrush(Color.Black), 2);
        for (int x = 0; x < 6; x++)
            graph.DrawLine(linePen, new Point(r.Next(0, 159), r.Next(0, 59)), new Point(r.Next(0, 159), r.Next(0, 59)));  

        //将图片保存到输出流中      
        basemap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
        context.Session["ImageUniqueCode"] = s.ToString();
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return true;
        }
    }

}
Attach
/FastFeedBack.ashx  【处理页面】
<%@ WebHandler Language="C#" Class="FastFeedback" %>

using System;
using System.Web;

public class FastFeedback : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    //最终状态
    public enum FeedbackState
    {
        UniqueCodeError = 1,
        Succeed = 5,
        Error = 10,
        Null = 20
    }
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.CacheControl = "no-cache"; //清空缓存
        
        string code = context.Request["SecurityCode"];
        string message = context.Request["Message"];

        if (string.IsNullOrEmpty(code) || string.IsNullOrEmpty(message))
        {
            context.Response.Write(FeedbackState.Null.ToString());
            context.Response.End();
            return; 
        }

        try
        {
            if (code.ToUpper() == context.Session["ImageUniqueCode"].ToString())
            {
                //执行成功,完成其他任务
                //....do.....
                context.Response.Write(FeedbackState.Succeed.ToString());

            }
            else
            {
                context.Response.Write(FeedbackState.UniqueCodeError.ToString());

            }
        }
        catch (Exception ex)
        {
            context.Response.Write(FeedbackState.Error.ToString());
        }
        finally
        {
            context.Response.End(); 
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}
View Code
1.C,下载地址返回顶部

 

warn作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
2025-07-18 08:12:09 192.168.2.138 GET /sysn/view/init/login.ashx - 6088 - 192.168.2.10 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/109.0.0.0+Safari/537.36 500 0 64 33347 2025-07-18 08:12:09 192.168.2.138 GET /sysn/view/init/login.ashx - 6088 - 192.168.2.10 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/109.0.0.0+Safari/537.36 500 0 64 16340 2025-07-18 08:12:09 192.168.2.138 GET /sysn/view/init/login.ashx - 6088 - 192.168.2.10 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/109.0.0.0+Safari/537.36 500 0 64 22329 2025-07-18 08:12:09 192.168.2.138 GET /sysn/view/init/login.ashx - 6088 - 192.168.2.10 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/109.0.0.0+Safari/537.36 500 0 64 33139 2025-07-18 08:12:09 192.168.2.138 GET /sysn/view/init/login.ashx - 6088 - 192.168.2.10 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/109.0.0.0+Safari/537.36 500 0 64 16220 2025-07-18 08:12:09 192.168.2.138 GET /sysn/view/init/login.ashx - 6088 - 192.168.2.10 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/109.0.0.0+Safari/537.36 500 0 64 27817 2025-07-18 08:12:09 192.168.2.138 GET /sysn/view/init/login.ashx - 6088 - 192.168.2.10 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/109.0.0.0+Safari/537.36 500 0 0 6834 2025-07-18 08:12:09 192.168.2.138 GET /sysn/view/init/login.ashx - 6088 - 192.168.2.10 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/109.0.0.0+Safari/537.36 500 0 64 28023 2025-07-18 08:12:09 192.168.2.138 GET /sysn/view/init/login.ashx - 6088 - 192.168.2.10 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/109.0.0.0+Safari/537.36 500 0 64 22546 分析日志
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值