C # session not persisting from HTTPHandler

I have an HTTPHandler that creates a Captcha image to the user whenever the captcha.ashx page is requested. The code for this is simple:

        CaptchaHandler handler = new CaptchaHandler();
        Random random = new Random();
        string[] fonts = new string[4] { "Arial", "Verdana", "Georgia", "Century Schoolbook" };
        string code = Guid.NewGuid().ToString().Substring(0, 5);
        context.Session.Add("Captcha", code);

        Bitmap imageFile = handler.GenerateImage(code, 100, 70, fonts[random.Next(0,4)]);
        MemoryStream ms = new MemoryStream();
        imageFile.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

        byte[] buffer = ms.ToArray();

        context.Response.ClearContent();
        context.Response.ContentType = "image/png";
        context.Response.BinaryWrite(buffer);
        context.Response.Flush();

      

Then on my regular website I got this:

...
<img id="securityCode" src="captcha.ashx" alt="" /><br />
<a href="javascript:void(0);" onclick="javascript:refreshCode();">Refresh</a>
...

      

This works great, the image is generated and sent back to the user whenever the captcha.ashx page is requested. My problem is that the HTTPHandler is not saving the session? I tried to return the session from a normal page, but I got an exception saying it doesn't exist, so I turned on Trace to see which sessions are active and it doesn't display the session that the HTTPHandler (Security Code) created.

HTTPHandler uses IReadOnlySessionState to interact with sessions. Does the HTTPHandler only have read access and therefore not save the session?

+2


source to share


2 answers


Try to implement the IRequiresSessionState interface from the namespace.



Check this link: http://anuraj.wordpress.com/2009/09/15/how-to-use-session-objects-in-an-httphandler/

+6


source


Your handler must implement IRequiresSessionState.



public class CaptchaHandler : IHttpHandler, IRequiresSessionState
{
...
}

      

+1


source







All Articles