【原】在一般处理程序中设置session

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;

namespace Itcast.Mall.WebApp.Handlers
{
    /// <summary>
    /// Vcode 的摘要说明
    /// </summary>
    public class Vcode : IHttpHandler,System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            var code = CaptchaHelper.CreateRandomCode(4);
            //类要实现System.Web.SessionState.IRequiresSessionState,这个才能设置session,否则会报错;
            context.Session["user_vcode"] = code;
            var img = CaptchaHelper.DrawImage(code, 20, background: Color.White);
            context.Response.ContentType = "image/gif";
            context.Response.BinaryWrite(img);

        }

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

 

posted @ 2015-04-26 22:28  哥本哈根  阅读(260)  评论(0)    收藏  举报