📄 authcodehttphandler.cs
字号:
using System;
using System.Web;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using System.IO;
using System.Web.UI;
using System.Text;
using NetFocus.Web.Core;
namespace NetFocus.Web.Applications.Forum
{
public class AuthCodeHttpHandler : IHttpHandler
{
int intLength = 4; //长度
string strIdentify = "AuthCode"; //随机字串存储键值,以便存储到Session中
public void ProcessRequest(HttpContext context)
{
try
{
//设置输出流图片格式
context.Response.ContentType = "image/gif";
Bitmap b = new Bitmap(70, 19);
Graphics g = Graphics.FromImage(b);
g.FillRectangle(new SolidBrush(ColorTranslator.FromHtml("#EDF2F7")), 0, 0, 70, 19);
Font font = new Font(FontFamily.GenericSerif, 16, FontStyle.Regular, GraphicsUnit.Pixel);
Random r = new Random();
//合法随机显示字符列表
string strLetters = "1234567890";
StringBuilder s = new StringBuilder();
//将随机生成的字符串绘制到图片上
for (int i = 0; i < intLength; i++)
{
s.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1));
g.DrawString(s[s.Length - 1].ToString(), font, new SolidBrush(ColorTranslator.FromHtml("#2265b2")), 3 + i * 16, r.Next(0, 1));
}
b.Save(context.Response.OutputStream, ImageFormat.Gif);
HttpCookie cookie = new HttpCookie(strIdentify, s.ToString());
cookie.Expires = DateTime.Now.AddDays(1);
context.Response.Cookies.Add(cookie);
context.Response.End();
}
catch { }
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -