How to use ASP.NET session state in HttpHandler?

I have an HttpHandler that runs on the client page (cross-domain, not on our IIS server, etc.), and when they click on our built-in link, it launches Handler on our server. So far, everything is working fine.

Now I am trying to use the System.Web.HttpContext.Session object, but it is zero. I think this is null because we do not have a session until our HttpHandler is called? And a few calls to the handler will create a new session for each call? If so, did MS simply disable the Session object when called in the HttpHandler? Can anyone confirm this?

If so, what do you do to maintain state between calls? Some kind of SQL based data object? File?

TIA

+50
session-state
Jul 09 '09 at 22:30
source share
4 answers

Ask HttpHandler to implement the IRequiresSessionState interface. This will allow you to use session state. IRequiresSessionState can be found in the System.Web.SessionState .

+123
Jul 09 '09 at 22:34
source

I think you need to implement an empty IReadOnlySessionState interface so that the context loads .

edit to add:

According to Michael Morton, you can also implement IRequiresSessionState , which will give you write access to the Session object as well

+24
Jul 09 '09 at 22:32
source
 using System; using System.Web; using System.Web.SessionState; public class DownloadHandler : IHttpHandler, IReadOnlySessionState { public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { context.Response.Write(context.Session["kmx"]); } } 
+2
Jan 30 '16 at 19:14
source

try using the current context ...

 System.Web.HttpContext.Current.Session 
-eleven
Jul 09 '09 at 22:36
source



All Articles