必须继承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)的更多相关文章

  1. 在Handler.ashx文件中使用session

    使用jquery调用handler文件中的方法,需要使用session,默认生成的文件中,不可以直接使用session.按照以下步骤,即可以通过session与其他的aspx页面的session进行数 ...

  2. ashx文件中使用session提示“未将对象引用设置到对象的实例”

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;u ...

  3. 解决在.ashx文件中判断Session 总是NULL的方法

    实现IHttpHandler接口的同时必须继承IRequiresSessionState接口,才能拿到session public class HttpHandler: IHttpHandler, I ...

  4. 2014-08-26 解决HttpContext.Current.Session在ashx文件中出现“未将对象引用设置到对象的实例”的问题

    今天是在吾索实习的第35天. 最近在使用HttpContext.Current.Session来获取Session["..."]的值时,常常会弹出错误——“未将对象引用设置到对象的 ...

  5. 在Visual Studio 的 “一般处理程序 ” .ashx 文件中如何创建Session 对象

    只需要继承这个接口即可实现创建Session对象.  IHttpHandler,System.Web.SessionState.IHttpSessionState 代码示例: public class ...

  6. asp.net 登陆后在ashx处理程序中获取不到Session

    登录后存储Session,另一个页面Ajax请求 ashx页面,发现无法获取到Session,Session is NULL  使用“IReadOnlySessionState”这个接口就可以

  7. ashx页面中context.Session["xxx"]获取不到值的解决办法

    在 aspx和aspx.cs中,都是以Session["xxx"]="aaa"和aaa=Session["xxx"].ToString()进 ...

  8. .NET .ashx 文件 用Session 是需要注意的问题

    .ashx 文件,默认不可使用 Session ,需要使用Session 时, 需要引用 接口 IRequiresSessionState 例如:  public class AddHouseInfo ...

  9. 分布式中使用Redis实现Session共享(二)

    上一篇介绍了一些redis的安装及使用步骤,本篇开始将介绍redis的实际应用场景,先从最常见的session开始,刚好也重新学习一遍session的实现原理.在阅读之前假设你已经会使用nginx+i ...

随机推荐

  1. MySQL中的group_concat函数

    group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符']) 以cat_id分组,把name字段的值打印在一行, ...

  2. 一个解析json串并组装echarts的option的函数解析

    缘起: 在组装echart页面的时候,遇到这样一个问题,它是一个需要在循环内层的时候,同时循环外层,在内层循环中就要将外层获取的值存入,导致了一种纠缠状态,费了老劲儿,终于得到如下解决.记录之,绿色为 ...

  3. 理解Compressed Sparse Column Format (CSC)

    最近在看<Spark for Data Science>这本书,阅读到<Machine Learning>这一节的时候被稀疏矩阵的存储格式CSC给弄的晕头转向的.所以专门写一篇 ...

  4. PHP进程通信基础——信号

    PHP进程通信基础--信号 使用信号通信.可以使用kill -l 来查看当前系统的信号类型. 每个信号所代表的的详细含义,请查看我的这篇博客:http://www.cnblogs.com/roverl ...

  5. python简单搭建HTTP Web服务器

    对于Python 2,简单搭建Web服务器,只需在i需要搭建Web服务器的目录(如C:/ 或 /home/klchang/)下,输入如下命令: python -m SimpleHTTPServer 8 ...

  6. php中双冒号::的用法

    注:本篇博客系转载,出处不可考(至少对我来说不可考...) 双冒号操作符即作用域限定操作符Scope Resolution Operator可以访问静态.const和类中重写的属性与方法. 在类定义外 ...

  7. laydate兼容bootstrap

    因为Bootstrap使用了 * {box-sizing:border-box;}来reset浏览器的默认盒子模型.所以加上以下代码就可以兼容Bootstrap了 <style type=&qu ...

  8. App Extension访问Cocoapods引入的第三方库

    步骤一: PROJECT --info --configurations,将对应的Debug和Release 设置成pods.debug和pods.release    步骤2:编译一下(本人遇到的问 ...

  9. 期权定价公式:BS公式推导——从高数和概率论角度

    嗯,自己看了下书.做了点笔记,做了一些相关的基础知识的补充,尽力做到了详细,这样子,应该上过本科的孩子,只要有高数和概率论基础.都能看懂整个BS公式的推导和避开BS随机微分方程求解的方式的证明了.

  10. 解决svn pritine text not exist问题

    svn: E155032: The pristine text with checksum '$sha1$151400d1cd4c5fc190d500aa1826d45cb91f088f' not f ...