在ashx文件中制作验证码(使用session要继承IRequiresSessionState)
必须继承System.Web.SessionState.IRequiresSessionState接口,才能实现Session读写!
System.Web.SessionState的一些接口
IReadOnlySessionState 指定目标 HTTP 处理程序只需要具有对会话状态值的读访问权限。这是一个标记接口,没有任何方法。
IRequiresSessionState 指定目标 HTTP 处理程序需要对会话状态值具有读写访问权。这是一个标记接口,没有任何方法。
using System;
using System.Web;
using System.Drawing;
using System.Web.SessionState;
public class CheckCode : IHttpHandler,IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string code = GenerateCheckCode();
context.Session["code"] = code;
this.CreateCheckCodeImage(code,context);
}
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 4; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
return checkCode;
}
private void CreateCheckCodeImage(string checkCode, HttpContext context)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 5; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
context.Response.ClearContent();
context.Response.ContentType = "image/Gif";
context.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
public bool IsReusable {
get {
return false;
}
}
}
页面显示时
<img src="ashx....."/>
判断验证码是否正确
Session["code"].ToString()==用户输入的文本框的值
写于 2014-04-01
在ashx文件中制作验证码(使用session要继承IRequiresSessionState)的更多相关文章
- 在Handler.ashx文件中使用session
使用jquery调用handler文件中的方法,需要使用session,默认生成的文件中,不可以直接使用session.按照以下步骤,即可以通过session与其他的aspx页面的session进行数 ...
- ashx文件中使用session提示“未将对象引用设置到对象的实例”
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;u ...
- 解决在.ashx文件中判断Session 总是NULL的方法
实现IHttpHandler接口的同时必须继承IRequiresSessionState接口,才能拿到session public class HttpHandler: IHttpHandler, I ...
- 2014-08-26 解决HttpContext.Current.Session在ashx文件中出现“未将对象引用设置到对象的实例”的问题
今天是在吾索实习的第35天. 最近在使用HttpContext.Current.Session来获取Session["..."]的值时,常常会弹出错误——“未将对象引用设置到对象的 ...
- 在Visual Studio 的 “一般处理程序 ” .ashx 文件中如何创建Session 对象
只需要继承这个接口即可实现创建Session对象. IHttpHandler,System.Web.SessionState.IHttpSessionState 代码示例: public class ...
- asp.net 登陆后在ashx处理程序中获取不到Session
登录后存储Session,另一个页面Ajax请求 ashx页面,发现无法获取到Session,Session is NULL 使用“IReadOnlySessionState”这个接口就可以
- ashx页面中context.Session["xxx"]获取不到值的解决办法
在 aspx和aspx.cs中,都是以Session["xxx"]="aaa"和aaa=Session["xxx"].ToString()进 ...
- .NET .ashx 文件 用Session 是需要注意的问题
.ashx 文件,默认不可使用 Session ,需要使用Session 时, 需要引用 接口 IRequiresSessionState 例如: public class AddHouseInfo ...
- 分布式中使用Redis实现Session共享(二)
上一篇介绍了一些redis的安装及使用步骤,本篇开始将介绍redis的实际应用场景,先从最常见的session开始,刚好也重新学习一遍session的实现原理.在阅读之前假设你已经会使用nginx+i ...
随机推荐
- dateRangePicker时间范围控件
Github:https://github.com/dangrossman/bootstrap-daterangepicker/ 使用daterangepicker()为元素创建一个时间范围控件 &l ...
- WebStorm 2016.2.3的安装与汉化
WebStorm是一款功能出色的JavaScript开发工具.号称是""Web前端开发神器"."最强大的HTML5编辑器"."最智能的Jav ...
- Junit的使用
Junit是用于编写单元测试的框架.对于已经写好的函数,可以使用Junit生成单元测试代码. 自己的环境是:Linux Java环境是:JDK1.7 IDE:Eclipse Java EE IDE f ...
- 关于 feature team 的一些内容
矩阵式管理,是常见的经典管理架构.其最早起源于美国的航空航天部门,然后被美国人带到了日本,然后被日本人带到了台湾,然后台湾人带到大陆...矩阵管理最典型的特征是,组织架构按职能与专业划分,项目由跨越部 ...
- 2016福州大学软件工程第三次个人作业-K米软件产品评测
K米软件测评个人作业结果统计如下: 评分标准: 按照栋哥布置的第三次个人作业--K米测评制定评分标准如下: 第一部分:调研.评测 下载并使用,描述最简单直观的个人第一次上手体验. 0.5 按照描述的b ...
- bzoj 4326: NOIP2015 运输计划
4326: NOIP2015 运输计划 Time Limit: 30 Sec Memory Limit: 128 MB Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n 个 ...
- JAVA中保留小数的多种方法
// 方式一:double f = 3.1516;BigDecimal b = new BigDecimal(f);double f1 = b.setScale(2, BigDecimal.ROUND ...
- IT这一行,如可高速下载国外资源之迅雷设置免费SSH代理下载国外资源
本文转自SUN'S BLOG 原文地址:IT这一行,如可高速下载国外资源之迅雷 我们这些做IT这一行的人,经常,下载一些国外的一些资源,可是让人蛋碎的是,往往这些资源下载都慢的像蜗牛,真的让人无法忍受 ...
- Linux学习之CentOS(二十)--CentOS6.4下修改MySQL编码方法
但是当我们在试图对数据库中的数据进行备份或者将sql文件导入到我们的数据库时可能就会碰到编码的问题,在windows下安装mysql时我们可以在安装的时候就选择好整个数据库的编码方式(通常设置成utf ...
- 12月15日smarty模板基本语法
smarty基本语法: 1.注释:<{* this is a comment *}>,注意左右分隔符的写法,要和自己定义的一致. <{* I am a Smarty comment, ...