ashx-auth-黑色简洁验证码

时间:2023-03-08 16:47:51
ashx-auth-黑色简洁验证码
ylbtech-util: ashx-auth-黑色简洁验证码

ashx-auth-黑色简洁验证码

1.A,效果图返回顶部
 ashx-auth-黑色简洁验证码
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(, );
Graphics graph = Graphics.FromImage(basemap);
graph.FillRectangle(new SolidBrush(Color.White), , , , );
Font font = new Font(FontFamily.GenericSerif, , FontStyle.Bold, GraphicsUnit.Pixel);
Random r = new Random();
string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";
string letter;
StringBuilder s = new StringBuilder(); //添加随机字符
for (int x = ; x < ; x++)
{
letter = letters.Substring(r.Next(, letters.Length - ), );
s.Append(letter);
graph.DrawString(letter, font, new SolidBrush(Color.Black), x * , r.Next(, ));
} //混淆背景
Pen linePen = new Pen(new SolidBrush(Color.Black), );
for (int x = ; x < ; x++)
graph.DrawLine(linePen, new Point(r.Next(, ), r.Next(, )), new Point(r.Next(, ), r.Next(, ))); //将图片保存到输出流中
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 = ,
Succeed = ,
Error = ,
Null =
} 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;
}
} }
1.C,下载地址返回顶部
ashx-auth-黑色简洁验证码 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。