ASP.NET:在一般处理程序中经过 Session 保存验证码却无法显示图片

ASP.NET:在一般处理程序中通过 Session 保存验证码却无法显示图片?

using System.Drawing;
using System.Web;
using System.Web.SessionState;

/// <summary>
/// CaptchaHandler 的摘要说明
/// </summary>
public class CaptchaHandler : IHttpHandler, IRequiresSessionState
{

  public void ProcessRequest(HttpContext context)
  {

    // GDI+ 三步 1画布 2为画布创建画笔 3绘制所需素材

    var vCode = CaptchaHelper.CreateRandomCode(5);  //自己封装的扩展方法

    var buffer = CaptchaHelper.DrawImage(vCode, background: Color.White);  //自己封装的扩展方法
    context.Session["vCode"] = vCode;  //vCode:string 类型的验证码字符串

    context.Response.ContentType = "image/gif";
    context.Response.BinaryWrite(buffer);
  }

  public bool IsReusable { get { return false; } }
}

 

【关键】Handler 要实现 IRequiresSessionState 接口(所在的命名空间 using System.Web.SessionState;)