views:

7869

answers:

4

I have a custom security principal object which I set in the global.asax for the current thread and all is well, no problems normally.

However, I'm just adding a dynamic image feature by having a page serve up the image and whenever that dynamic image page is loaded the System.Web.HttpContext.Current.Session is null in global.asax which prevents me from setting the security principal as normal and cascading problems from that point onwards.

Normally the Session is null in global.asax only once during a session at the start when the user logs in, afterwards it's always available with this single exception.

The dynamic image page is loaded when the browser comes across an image tage in the original page i.e.

I'm guessing that this is some aspect of the fact that the browser is requesting that page without sending some credentials with it?

Any help would be greatly appreciated.

+2  A: 

Session has nothing to do with being logged in or not.

What event are you overriding when you want access to the session? Session isn't available until AcquireRequestState has been fired.

For more information, see: http://msdn.microsoft.com/en-us/library/9ysfzy8h.aspx

Brad Wilson
Session has everything to do with my custom business principle. The problem lies in that session is null only for this one page, the 50 or so others in this large application do not exhibit this issue. I access Session from Application_PostAuthenticateRequest where it's normally not an issue.
JohnC
For the sake of this issue, just ignore the whole business principal part, the fundamental problem is that Session is null only for this one type of page.
JohnC
Jon what type of page is it? Is this an aspx? or ashx?
JoshBerke
+9  A: 

John,

I'm assuming you're using an ashx handler for the handler. If so, be sure to derive from IRequiresSessionState for example:

public class Images : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{ }

If you're not using an ashx can you describe what you mean by dynamic image page?

Josh

JoshBerke
Hi Josh, You're my favorite person right now! :) No I was using a standard aspx page because all the examples about handlers mentioned using a file extension and config changes, didn't know about ashx at all, tried it, and it did require the derivation you mentioned. Works perfectly. Cheers!
JohnC
Heh awsome! I love ashx handlers. I've used one to pull images from a database which works really well. I've also found that if your doing a POX Service that the ashx works out very well.
JoshBerke
A: 

yes you are right This happens because the object dependancy might conficts in case of other page transferance parallel which may break down the firewall between sessions

+1  A: 

in Global.asax.cs Session_Start() and Session_End() you need to use "this.Session" !! The reason for this is that HttpContext is only available when there is a request that is being processed. That is why you are getting a NULL on HttpContext.Current.Session!

From Microsoft website: "HttpContext Class: Encapsulates all HTTP-specific information about an individual HTTP request."

But don't feel bad ... i fell for this one too! :)