J
jvcoach23
I have some code that when published to an IIS 6 box it works, but IIS 7 it
does not. It has to do with cookies. Have some code in a SessionModule that
when the client comes to the site, a cookie is created for the duration of
their session. The code looks something like this
Dim ctx As HttpContext = HttpContext.Current
Dim cookie As HttpCookie = ctx.Request.Cookies("TestIt")
If cookie Is Nothing Then
Dim newGuid As String = Guid.NewGuid().ToString
Dim mTimeTick As String = System.DateTime.Now.Ticks
Dim mSmashIt As String = newGuid & "_" & mTimeTick
ctx.Response.Cookies.Add(New HttpCookie("TestIt", mSmashIt))
End If
When i try retrieve the cookie there is an object reference is nothing. If
i create a cookie in the aspx page, it will create a cookie, just not in the
SessionModule. So is there something that has to be done in IIS 7 to get
this to work.
I'll include code now below to give more detail.. but that is the issue i'm
running into.
--in the web config
<httpModules>
<add name="SessionModule" type="SessionModule,App_code"/>
--session module class
Imports System
Imports System.Web
Public Class SessionModule
Implements IHttpModule
#Region "IHttpModule Members"
Public Sub Init(ByVal context As HttpApplication) Implements
IHttpModule.Init
AddHandler context.AuthenticateRequest, AddressOf
OnAuthenticateRequest
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
#End Region
#Region "Event Handler"
Private Sub OnAuthenticateRequest(ByVal sender As Object, ByVal e As
EventArgs)
Dim ctx As HttpContext = HttpContext.Current
Dim cookie As HttpCookie = ctx.Request.Cookies("SessionGUID")
If cookie Is Nothing Then
'added these to get around some duplication that was happening
when just using the Guid.NewGuid()
Dim newGuid As String = Guid.NewGuid().ToString
Dim mTimeTick As String = System.DateTime.Now.Ticks
Dim mSmashIt As String = newGuid & "_" & mTimeTick
'ctx.Response.Cookies.Add(New HttpCookie("SessionGUID",
Guid.NewGuid().ToString()))
ctx.Response.Cookies.Add(New HttpCookie("SessionGUID", mSmashIt))
End If
End Sub
#End Region
End Class
--and in the aspx.vb page to retrieve it
Dim ctx As HttpContext = HttpContext.Current
Dim cookie As HttpCookie = ctx.Request.Cookies("SessionGUID")
Try
Me.lblHead.Text = cookie.Value
Catch ex As Exception
Me.lblHead.Text = ex.Message.ToString
End Try
once again.. this works in IIS6 but not 7. i can create a cookie in the
aspx.vb page and retrieve it. I just can't figure out what needs to be done
to get teh IIS 7 box to create the cookie.
thanks
shannon
does not. It has to do with cookies. Have some code in a SessionModule that
when the client comes to the site, a cookie is created for the duration of
their session. The code looks something like this
Dim ctx As HttpContext = HttpContext.Current
Dim cookie As HttpCookie = ctx.Request.Cookies("TestIt")
If cookie Is Nothing Then
Dim newGuid As String = Guid.NewGuid().ToString
Dim mTimeTick As String = System.DateTime.Now.Ticks
Dim mSmashIt As String = newGuid & "_" & mTimeTick
ctx.Response.Cookies.Add(New HttpCookie("TestIt", mSmashIt))
End If
When i try retrieve the cookie there is an object reference is nothing. If
i create a cookie in the aspx page, it will create a cookie, just not in the
SessionModule. So is there something that has to be done in IIS 7 to get
this to work.
I'll include code now below to give more detail.. but that is the issue i'm
running into.
--in the web config
<httpModules>
<add name="SessionModule" type="SessionModule,App_code"/>
--session module class
Imports System
Imports System.Web
Public Class SessionModule
Implements IHttpModule
#Region "IHttpModule Members"
Public Sub Init(ByVal context As HttpApplication) Implements
IHttpModule.Init
AddHandler context.AuthenticateRequest, AddressOf
OnAuthenticateRequest
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
#End Region
#Region "Event Handler"
Private Sub OnAuthenticateRequest(ByVal sender As Object, ByVal e As
EventArgs)
Dim ctx As HttpContext = HttpContext.Current
Dim cookie As HttpCookie = ctx.Request.Cookies("SessionGUID")
If cookie Is Nothing Then
'added these to get around some duplication that was happening
when just using the Guid.NewGuid()
Dim newGuid As String = Guid.NewGuid().ToString
Dim mTimeTick As String = System.DateTime.Now.Ticks
Dim mSmashIt As String = newGuid & "_" & mTimeTick
'ctx.Response.Cookies.Add(New HttpCookie("SessionGUID",
Guid.NewGuid().ToString()))
ctx.Response.Cookies.Add(New HttpCookie("SessionGUID", mSmashIt))
End If
End Sub
#End Region
End Class
--and in the aspx.vb page to retrieve it
Dim ctx As HttpContext = HttpContext.Current
Dim cookie As HttpCookie = ctx.Request.Cookies("SessionGUID")
Try
Me.lblHead.Text = cookie.Value
Catch ex As Exception
Me.lblHead.Text = ex.Message.ToString
End Try
once again.. this works in IIS6 but not 7. i can create a cookie in the
aspx.vb page and retrieve it. I just can't figure out what needs to be done
to get teh IIS 7 box to create the cookie.
thanks
shannon