Home » ASP » ASP.Net » How to access Session information from HttpHandler (Ashx file)

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.

One Response so far.

  1. […] HttpHandler was there in Asp.Net traditional and in Asp.Net MVC is even easier because not configuration is required for IIS. […]

Leave a Reply

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