Preserve Session Variables Across HttpHandlers

2019-07-21 17:55发布

问题:

I have an ASP.NET application with 5 .ashx HTTPHandlers that implement IRequiresSessionState or IReadOnlySessionState.

Upon calling the first handler I create a variable and store it in the session.

When I call the next HttpHandler the variable is not available in the session object.

I am use context.Session.

I have a global.asax where I retrieve the sessionId.

Is there a way to preserve session variables across HttpHandlers or does each Handler get its own session?

回答1:

HeartAttack has a good discussion of this here:

http://weblogs.asp.net/ashicmahtab/archive/2008/09/18/how-to-use-session-values-in-an-httphandler.aspx

Basically, you need to do something like:

public class MyHandler:IHttpHandler,IRequiresSessionState

HTH's.

This should maintain session state across handlers. That is, when you log in, you only have one session for all of your requests into that domain. I'd suggest verifying that you are really logged in. Cache of course will work because that is common to all sessions but much more expensive on the server because it does not easily go away.