Search code examples
asp.netsessioncookieshttphandlerashx

Make HTTPHandler (ashx) to place session cookie in ASP.NET


How can I make a HTTPHandler in an ashx file PLACE a SessionCookie managed by ASP.NET? It seems, that the call of an ashx-File does not place a cookie, so no session can be maintained.

Background: I am about to write HTTP based web service wrapper. Different clients call the URL I provide them with the given parameters. I check the credentials, call the 3rd party web service and deliver the changed result back to the client. The 3rd party web service provider demands by contract a SessionID for every different client calling.

Solution: An HTTPHandler seems to be the best choice. So I created a Web Site Project. Unfortunately a direct call of a ashx-File seems not to add a session cookie to the response, so a new SessionID is being created for every call from the same client.

I did the following relevant:

  • Implemented markup interfaces IRequiresSessionState or/and IReadOnlySessionState
  • Added <sessionState mode="InProc" cookieless="false" timeout="20" /> to the web.config
  • Save something in the session state in the Global.asax File in the Session_Start-Method HttpContext.Current.Session("init") = 0

Different attempts which work, but should be avoided:

  • Before calling the ashx-File a call of the Default.aspx File places a session cookie, so the session is being maintained.
  • Implement an “own” Session with a database and a timestamp.

Version: IIS 6.0


Solution

  • IRequireSessionState and IRequireReadOnlySessionState are mutually exclusive. You should only be implementing the former if you intend to update session state in any way.