How to access Session information from HttpHandler (Ashx file)

You may pass manually the information but would it be easier if the http handler could read the session information?

This can be done with .Net Framework easily. In theory, this should not be a problem because the handler is in the server. It only needs to provide the session of the current user. To do, you need to use special interface that will allow you to reach this information.

This interface is called IReadOnlySessionState or IRequiresSessionState.

Here is an example:


<% @ webhandler language="C#" class="MyClass" %>

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

public class MyClass: IHttpHandler, IReadOnlySessionState
{
   public bool IsReusable { get { return true; } }

   public void ProcessRequest(HttpContext ctx)
   {
       ctx.Response.Write(ctx.Session["ID"]);
   }
}

This example show you how to get the ID session’s variable from the HttpHandler.

The second interface IRequiresSessionState is the same but will let you write also in the session.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

1 thought on “How to access Session information from HttpHandler (Ashx file)

  1. Pingback: Patrick Desjardins' Blog » How to do a custom secured file access with Http Handler and MVC framework

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.