I've already checked and objDR("MemID") is not NULL and neither is objDR("lastLogin"). strSessionVar contains a valid string (something like "ab_SessionInfo"). httpContext.Current.Session, however, IS Nothing....
OK. One detail I left out: I'm trying to access the session in the Application.PreRequestHandlerExecute event. I have a comment in that event that says I'm specifically doing it there because session is not available in BeginRequest or AuthorizeRequest.
Is this something that changed with IIS7? (I'm pretty sure this code used to work -- stumbled upon it in testing/debugging other code)...
I avoid sessions like the plague. However, some 3rd party code needs it so that it will integrate with the rest of our site. I'll try moving
the code to the Application.PostAcquireRequestState like the link you sent me shows.
Hmmm. The technique is not working (might be the translation to vb.net: here's what I have:
Public Class SitesHttpModule
Implements IHttpModule
'Lots of code not relevant to this discussion
Private Sub Application_PostAcquireRequestState(ByVal sender As Object, ByVal e As System.EventArgs) Handles Application.PostAcquireRequestState
Dim objContext As HttpContext = HttpContext.Current
Dim resourceHttpHandler As ForceSessionLoadHandler = TryCast(objContext.Handler, ForceSessionLoadHandler)
If Not (resourceHttpHandler Is Nothing) Then
objContext.Handler = resourceHttpHandler._OriginalHandler
End If
End Sub
Private Sub Application_PostMapRequestHandler(sender As Object, e As System.EventArgs) Handles Application.PostMapRequestHandler
Dim app As HttpApplication = CType(sender, HttpApplication)
Dim objIROSS As IReadOnlySessionState = TryCast(app.Context.Handler, IReadOnlySessionState)
Dim objIRSS As IRequiresSessionState = TryCast(app.Context.Handler, IRequiresSessionState)
If ((Not (objIROSS Is Nothing)) Or (Not (objIRSS Is Nothing))) Then
Exit Sub
End If
app.Context.Handler = New ForceSessionLoadHandler(app.Context.Handler)
End Sub
Private Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As System.EventArgs) Handles Application.PreRequestHandlerExecute
Dim objContext As HttpContext = HttpContext.Current
'Code that attepmpts to access the session -- It's "Nothing" here
End Class
Public Class ForceSessionLoadHandler
Implements IHttpHandler, IRequiresSessionState
Friend ReadOnly _OriginalHandler As IHttpHandler
Public Sub New(ByRef originalHandler As IHttpHandler)
_OriginalHandler = originalHandler
End Sub
Public ReadOnly Property IsReusable As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Public Sub ProcessRequest(context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
Throw New InvalidOperationException("ForceSessionLoadHandler cannot process requests.")
End Sub
End Class
I am having the same issue with a custom authentication module. My translation is similar to ojm37's and still no luck. What does the INIT look like on these module?
I am dealing with:
Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
AddHandler context.AuthenticateRequest, AddressOf Me.AuthenticateUser
AddHandler context.EndRequest, AddressOf Me.IssueAuthenticationChallenge
AddHandler context.PostAcquireRequestState, New EventHandler(AddressOf Application_PostAcquireRequestState)
AddHandler context.PostMapRequestHandler, New EventHandler(AddressOf Application_PostMapRequestHandler)
End Sub
Hmmm. This issue only happens in Integrated pipeline mode when the resource being requested on the URL is NOT an aspx page (for example, a jpg image). In my case, all I needed to do was skip the code being run when it was not an aspx file (since what I'm
doing is only valid when an aspx is being processed).
Not sure why the code translation did not work. Probably something about the "IS" operator in C# not having an exact equivalent in vb. At least, I couldn't find one.
ojm37
Contributor
2248 Points
832 Posts
Creating a Session Variable in an httpModule: NullReferenceException
Jun 02, 2011 06:42 PM|LINK
I'm trying to create a session variable (for a 3rd party application) in the httpModule for our site and I'm getting a NullReferenceException.
Example code:
httpContext.Current.Session(strSessionVar) = objDR("MemID").ToString & Chr(1) & objDR("lastLogin").ToStringI've already checked and objDR("MemID") is not NULL and neither is objDR("lastLogin"). strSessionVar contains a valid string (something like "ab_SessionInfo"). httpContext.Current.Session, however, IS Nothing....
Any ideas?
nobdy
Contributor
5744 Points
1036 Posts
Re: Creating a Session Variable in an httpModule: NullReferenceException
Jun 02, 2011 06:52 PM|LINK
You can not directly access HttpContext.Current as it will be null.
Try this post
http://stackoverflow.com/questions/276355/can-i-access-session-state-from-an-httpmodule
Let me know if that works.
My Blog | My Website
ojm37
Contributor
2248 Points
832 Posts
Re: Creating a Session Variable in an httpModule: NullReferenceException
Jun 02, 2011 06:56 PM|LINK
Hmmm. httpContext.Current is NOT NULL, but the Session is.
Thanks for the link. Digesting it now....
nobdy
Contributor
5744 Points
1036 Posts
Re: Creating a Session Variable in an httpModule: NullReferenceException
Jun 02, 2011 07:02 PM|LINK
It can be null in some cases Depending upon how request is made.
My Blog | My Website
ojm37
Contributor
2248 Points
832 Posts
Re: Creating a Session Variable in an httpModule: NullReferenceException
Jun 02, 2011 07:06 PM|LINK
OK. One detail I left out: I'm trying to access the session in the Application.PreRequestHandlerExecute event. I have a comment in that event that says I'm specifically doing it there because session is not available in BeginRequest or AuthorizeRequest. Is this something that changed with IIS7? (I'm pretty sure this code used to work -- stumbled upon it in testing/debugging other code)...
nobdy
Contributor
5744 Points
1036 Posts
Re: Creating a Session Variable in an httpModule: NullReferenceException
Jun 02, 2011 07:13 PM|LINK
Might be.. I make very less use of sessions.
May be saving session in SQL works in these Events.
My Blog | My Website
ojm37
Contributor
2248 Points
832 Posts
Re: Creating a Session Variable in an httpModule: NullReferenceException
Jun 02, 2011 07:15 PM|LINK
I avoid sessions like the plague. However, some 3rd party code needs it so that it will integrate with the rest of our site.
I'll try moving
the code to the Application.PostAcquireRequestState like the link you sent me shows.
ojm37
Contributor
2248 Points
832 Posts
Re: Creating a Session Variable in an httpModule: NullReferenceException
Jun 02, 2011 09:21 PM|LINK
Hmmm. The technique is not working (might be the translation to vb.net: here's what I have:
Public Class SitesHttpModule Implements IHttpModule 'Lots of code not relevant to this discussion Private Sub Application_PostAcquireRequestState(ByVal sender As Object, ByVal e As System.EventArgs) Handles Application.PostAcquireRequestState Dim objContext As HttpContext = HttpContext.Current Dim resourceHttpHandler As ForceSessionLoadHandler = TryCast(objContext.Handler, ForceSessionLoadHandler) If Not (resourceHttpHandler Is Nothing) Then objContext.Handler = resourceHttpHandler._OriginalHandler End If End Sub Private Sub Application_PostMapRequestHandler(sender As Object, e As System.EventArgs) Handles Application.PostMapRequestHandler Dim app As HttpApplication = CType(sender, HttpApplication) Dim objIROSS As IReadOnlySessionState = TryCast(app.Context.Handler, IReadOnlySessionState) Dim objIRSS As IRequiresSessionState = TryCast(app.Context.Handler, IRequiresSessionState) If ((Not (objIROSS Is Nothing)) Or (Not (objIRSS Is Nothing))) Then Exit Sub End If app.Context.Handler = New ForceSessionLoadHandler(app.Context.Handler) End Sub Private Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As System.EventArgs) Handles Application.PreRequestHandlerExecute Dim objContext As HttpContext = HttpContext.Current 'Code that attepmpts to access the session -- It's "Nothing" here End Class Public Class ForceSessionLoadHandler Implements IHttpHandler, IRequiresSessionState Friend ReadOnly _OriginalHandler As IHttpHandler Public Sub New(ByRef originalHandler As IHttpHandler) _OriginalHandler = originalHandler End Sub Public ReadOnly Property IsReusable As Boolean Implements System.Web.IHttpHandler.IsReusable Get Return False End Get End Property Public Sub ProcessRequest(context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest Throw New InvalidOperationException("ForceSessionLoadHandler cannot process requests.") End Sub End ClassSenator Bing...
Member
2 Points
1 Post
Re: Creating a Session Variable in an httpModule: NullReferenceException
Jun 03, 2011 02:30 PM|LINK
I am having the same issue with a custom authentication module. My translation is similar to ojm37's and still no luck. What does the INIT look like on these module?
I am dealing with:
Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init AddHandler context.AuthenticateRequest, AddressOf Me.AuthenticateUser AddHandler context.EndRequest, AddressOf Me.IssueAuthenticationChallenge AddHandler context.PostAcquireRequestState, New EventHandler(AddressOf Application_PostAcquireRequestState) AddHandler context.PostMapRequestHandler, New EventHandler(AddressOf Application_PostMapRequestHandler) End SubChris
ojm37
Contributor
2248 Points
832 Posts
Re: Creating a Session Variable in an httpModule: NullReferenceException
Jun 06, 2011 05:34 PM|LINK
Hmmm. This issue only happens in Integrated pipeline mode when the resource being requested on the URL is NOT an aspx page (for example, a jpg image). In my case, all I needed to do was skip the code being run when it was not an aspx file (since what I'm doing is only valid when an aspx is being processed).
Not sure why the code translation did not work. Probably something about the "IS" operator in C# not having an exact equivalent in vb. At least, I couldn't find one.
Thanks,