ashx-auth-黑色简洁验证码

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/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2013-09-13 17:12  ylbtech  阅读(819)  评论(2编辑  收藏  举报