httpCookie is null

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
 
G

Gregory A. Beamer

jvcoach23 said:
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

Shannon:

I would examine how the session bits are handled in IIS 7 if you need this
type of cookie, but I am not convinced you do. If storing start time is your
main goal, then using session could solve the problem just as well. If you
are tying values to a unique "ID", then you can use the Session ID that is
generated when a session is formed, as it is available with each hit to the
site. It is a nice unique id to link to. Perhaps I am missing the intent
here?

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
| Think outside the box! |
************************************************
 
J

jvcoach23

Thanks for the reply.

What i've after is keeping track of a user for the duration of their vist.
Once i had created the cookie, when i logged quite a few things while they
are at the site, i would attach that cookie value to it. If they opened up a
session and went to ten pages one vist, then later on that day came back to
the site for another 20 pages, i wanted to record that as 2 seperate vists, 2
seperate cookie values. I was using the sesionModule route with an
httpcookie because i had read somewhere that that is how it's done.

So your saying that instead of using a httpcookie, that i should use a
session instead. would this be as simple as just replacing where the cookie
was being created in the sessionmodule with a session?

Thanks
shannon
 
J

jvcoach23

well.. i'm missing something likely.. but just trying to do a session doesn't
work. It's not availble at this point.. in the SessionModule.

Hope your still in the mood to lend a hand.
thanks
 
J

jvcoach23

well.. i didn't figure it out.. but did somethig different that does work..
not sure if it's better or worse.

I took out all the plumbing for the session module and instead created a
session in the global.ascx file in the session_start... maybe that is where
it should have been all along. from that point i was able to change teh
spots where i was using the cookie to the session.

works as far as i can tell.. more testing will tell.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top