Google

How to access Session information from HttpHandler (Ashx file)

Written on:October 18, 2011
Comments
Add One

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 Comment add one

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

Leave a Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>