ashx-auth-黑色簡潔驗證碼

ylbtech-util: ashx-auth-黑色簡潔驗證碼

 ashx-auth-黑色簡潔驗證碼緩存

1.A,效果圖返回頂部
 
1.B,源代碼返回頂部

/ImageUniqueCode.ashxdom

<%@ 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/
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。
相關文章
相關標籤/搜索