I have an interesting problem. How, or when can I access SessionState from
the HttpModule?
It seems that session state is only available when *.aspx page is requested.
It is not available when other, "static" resources, like *.gif files are handled.
Is there any way I can force session state to load, even when it is not
*.aspx page being requested?
What I am trying to develop is a special kind of rewrite engine. It must
have access to session state.
It looks like it is a known issue and whether or not session state is available is actually decided... by the SessionStateModule itself!
Briefly, in order to force session state to load current Context.Handler, usually DefaultHttpHandler, has to be replaced in the PostMapRequestHandler event by some other handler, implementing IRequiresSessionState. To be safe, my dummy handler carries a
reference to the original handler, and I swap it back in the PostAcquireRequestState event, once the dummy handler fulfilled its purpose.
I have the same issue. I need to access some session variables but I can't get it to work. Can you please elaborate on your comments above or include a small code block?
I just recently came across the same issue but cant say exactly wat u looking is really i am going to answer, i read it somewhere that managed and unmanaged resources in asp.net handles session differently, means there is an interface named IRequireSessionState
which is been implemented in managed resource and in unmanaged resources like pictures...etc they dont come into it caz as the microsoft guy was saying that these static(unmanaged) resources can be put directly onto the server or it can be saved in any format
across the request(i really didnt get it how to save it in during request but anywys.....[:)] ) i came up wiv a solution , infact i was into some kind of multiple login stuff and how to prevent it i came across some good stuff which was using cache object
to make up a key and try to chk that cache before letting u in the application ...... and there were some pages and user controls where i was using pictures and some resources......etc and whenever i try to access the unmanaged resource it always throws this
erorr session state context is not available in present context.... blah blah....
So i come up with the solution which took lil bit time but it was worth a while to search for it.....I normally belief in piece of code will really helps the developer community caz there is always a communication gap and piece of code can really lesser
this gap.... if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)
{
Exception lastException = Server.GetLastError;
if (!((lastException == null))) {
Session("ErrorStack") = lastException.StackTrace;
Session("ErrorSource") = lastException.Source;
Session("ErrorMessage") = lastException.Message;
} else {
if (!((Session("AnyName") == null))) {
ClassName up;
up = Session("AnyName");
string sKey = ((string)(up.UserID));
string sUser = ((string)(HttpContext.Current.Cache(sKey)));
}
}
}
It was in global.asax page and i did find a way to ignore the unmanaged resource by simply chking that if present context handler is implementing or is type of
IRequiresSessionState and IReadOnlySessionState simply ignore them, this way i did resolve my issue....
Again i cant exactly say that how it can be really helpful to u but as all pattern are have baked... u have to figure out how it can help u.
Interesting information. Actually my issue was accessing the session variables from pages as well from within an httpmodule. I found out that I had to add a handler for
PreRequestHandlerExecute to be able to have a valid (non-null) context containing the session data. This post helped me out:
Sorry if my issue veers away from the original poster's question but it looks like alot of people are looking for the same information I was looking for.
tdjastrzebsk...
Member
320 Points
108 Posts
SessionState in HttpModule problem (2.0)
Apr 13, 2007 11:11 AM|LINK
Hello Developers,
I have an interesting problem. How, or when can I access SessionState from
the HttpModule?
It seems that session state is only available when *.aspx page is requested.
It is not available when other, "static" resources, like *.gif files are handled.
Is there any way I can force session state to load, even when it is not
*.aspx page being requested?
What I am trying to develop is a special kind of rewrite engine. It must
have access to session state.
Thank you,
Tomasz J
SessionState HttpModule
deepak11
Member
291 Points
51 Posts
Re: SessionState in HttpModule problem (2.0)
Apr 13, 2007 02:50 PM|LINK
You can access the session state in the page_PreInit event handler in the HttpModule.
To access it you do: HttpContext.Current.Session
|My website|
Please “mark as answer” if this answers your question. Thanks.
tdjastrzebsk...
Member
320 Points
108 Posts
Re: SessionState in HttpModule problem (2.0)
Apr 13, 2007 11:26 PM|LINK
Page event in HttpModule?? There is no page when *.gif file is served.
tdjastrzebsk...
Member
320 Points
108 Posts
Re: SessionState in HttpModule problem (2.0)
Apr 14, 2007 02:16 PM|LINK
I just came across this great post by Mike Volodarsky, IIS Core Server / ASP.NET Runtime Program Manager
https://forums.iis.net/thread/1648944.aspx
It looks like it is a known issue and whether or not session state is available is actually decided... by the SessionStateModule itself!
Briefly, in order to force session state to load current Context.Handler, usually DefaultHttpHandler, has to be replaced in the PostMapRequestHandler event by some other handler, implementing IRequiresSessionState. To be safe, my dummy handler carries a reference to the original handler, and I swap it back in the PostAcquireRequestState event, once the dummy handler fulfilled its purpose.
Tomasz J
rdionne11
Member
46 Points
16 Posts
Re: SessionState in HttpModule problem (2.0)
Apr 20, 2007 07:14 PM|LINK
Hi,
I have the same issue. I need to access some session variables but I can't get it to work. Can you please elaborate on your comments above or include a small code block?
Thanks
I have the same issue.
coolrah
Member
4 Points
3 Posts
Re: SessionState in HttpModule problem (2.0)
Apr 22, 2007 05:56 AM|LINK
Hi Tomasz,
I just recently came across the same issue but cant say exactly wat u looking is really i am going to answer, i read it somewhere that managed and unmanaged resources in asp.net handles session differently, means there is an interface named IRequireSessionState which is been implemented in managed resource and in unmanaged resources like pictures...etc they dont come into it caz as the microsoft guy was saying that these static(unmanaged) resources can be put directly onto the server or it can be saved in any format across the request(i really didnt get it how to save it in during request but anywys.....[:)] ) i came up wiv a solution , infact i was into some kind of multiple login stuff and how to prevent it i came across some good stuff which was using cache object to make up a key and try to chk that cache before letting u in the application ...... and there were some pages and user controls where i was using pictures and some resources......etc and whenever i try to access the unmanaged resource it always throws this erorr session state context is not available in present context.... blah blah....
So i come up with the solution which took lil bit time but it was worth a while to search for it.....I normally belief in piece of code will really helps the developer community caz there is always a communication gap and piece of code can really lesser this gap....
if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState){
Exception lastException = Server.GetLastError;
if (!((lastException == null))) {
Session("ErrorStack") = lastException.StackTrace;
Session("ErrorSource") = lastException.Source;
Session("ErrorMessage") = lastException.Message;
} else {
if (!((Session("AnyName") == null))) {
ClassName up;
up = Session("
AnyName");string sKey = ((string)(up.UserID));
string sUser = ((string)(HttpContext.Current.Cache(sKey)));
}
}
}
It was in global.asax page and i did find a way to ignore the unmanaged resource by simply chking that if present context handler is implementing or is type ofIRequiresSessionState andIReadOnlySessionState simply ignore them, this way i did resolve my issue....Again i cant exactly say that how it can be really helpful to u but as all pattern are have baked... u have to figure out how it can help u.RegardsRahmanSession State httpcontext Seession Session_End HttpModule
rdionne11
Member
46 Points
16 Posts
Re: SessionState in HttpModule problem (2.0)
Apr 23, 2007 04:15 PM|LINK
PreRequestHandlerExecute to be able to have a valid (non-null) context containing the session data. This post helped me out:
http://channel9.msdn.com/ShowPost.aspx?PostID=1498
Sorry if my issue veers away from the original poster's question but it looks like alot of people are looking for the same information I was looking for.
tdjastrzebsk...
Member
320 Points
108 Posts
Re: SessionState in HttpModule problem (2.0)
Apr 24, 2007 06:15 PM|LINK
Here is the sample code.
Regrads,
Tomasz J
using System;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Diagnostics;
// This code demonstrates how to make session state available in HttpModule,
// regradless of requested resource.
// author: Tomasz Jastrzebski
public class MyHttpModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.PostAcquireRequestState += new EventHandler(Application_PostAcquireRequestState);
application.PostMapRequestHandler += new EventHandler(Application_PostMapRequestHandler);
}
void Application_PostMapRequestHandler(object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
if (app.Context.Handler is IReadOnlySessionState || app.Context.Handler is IRequiresSessionState) {
// no need to replace the current handler
return;
}
// swap the current handler
app.Context.Handler = new MyHttpHandler(app.Context.Handler);
}
void Application_PostAcquireRequestState(object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
MyHttpHandler resourceHttpHandler = HttpContext.Current.Handler as MyHttpHandler;
if (resourceHttpHandler != null) {
// set the original handler back
HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler;
}
// -> at this point session state should be available
Debug.Assert(app.Session != null, "it did not work :(");
}
public void Dispose()
{
}
// a temp handler used to force the SessionStateModule to load session state
public class MyHttpHandler : IHttpHandler, IRequiresSessionState
{
internal readonly IHttpHandler OriginalHandler;
public MyHttpHandler(IHttpHandler originalHandler)
{
OriginalHandler = originalHandler;
}
public void ProcessRequest(HttpContext context)
{
// do not worry, ProcessRequest() will not be called, but let's be safe
throw new InvalidOperationException("MyHttpHandler cannot process requests.");
}
public bool IsReusable
{
// IsReusable must be set to false since class has a member!
get { return false; }
}
}
}
rdionne11
Member
46 Points
16 Posts
Re: SessionState in HttpModule problem (2.0)
May 17, 2007 10:52 PM|LINK
Thanks for the code. It's a bit better than the code I came up with.
ojm37
Contributor
2248 Points
832 Posts
Re: SessionState in HttpModule problem (2.0)
Jun 02, 2011 09:37 PM|LINK
Translate to VB.Net anyone?
I made an attempt, but it's not working...