by Andreas

.NET HTTP Handlers (.ashx) – How to get access to Session

.NET HTTP Handlers (.ashx) are really useful for jQuery / JSON communication, but if you are using these as part of a bigger web application you will find that you don’t have immediate access to the current Session data. In a recent project I needed to validate some of the current user’s session information before allowing the handler to be invoked. In order to get access to HttpContext.Current.Session from the System.Web namespace you’ll have to add the System.Web.SessionState.IReadOnlySessionState interface in the class definition:


This will allow you to access the current session data:

// get session data
var sessionVariable = HttpContext.Current.Session["sessionVariableName"];
  • Njål

    Tip: If you need to change some Session variables then implement IRequiresSessionState instead of IReadOnlySessionState