Home » General Scripting and Coding » ASP.Net » Getting and Setting Session State in HttpHandlers (ASHX files)

Getting and Setting Session State in HttpHandlers (ASHX files)

Posted on by

If you just want to read your Session State from an ASHX or HttpHandler, you need to implement IReadOnlySessionState.  If you want to write to your Session State, you must implement IRequiresSessionState.  Note that you will also need use the System.Web.SessionState namespace.  This post describes getting and setting session state in HttpHandlers (ASHX files).

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

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

public class UploadFile : IHttpHandler, IRequiresSessionState
{
   public bool IsReusable { get { return true; } }
   
   public void ProcessRequest(HttpContext context)
   {
       string ftpUsername = "ftpUser";
       context.Session["ftpUsername"] = ftpUsername;
   }
}

 

Comments have now been disabled. If you have a question to ask about this post please ask the community!