httpCookie is null

Discussion in 'ASP .Net' started by jvcoach23, Apr 28, 2010.

  1. jvcoach23

    jvcoach23 Guest

    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
    jvcoach23, Apr 28, 2010
    #1
    1. Advertising

  2. "jvcoach23" <> wrote in message
    news:...
    > 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! |
    ************************************************
    Gregory A. Beamer, Apr 29, 2010
    #2
    1. Advertising

  3. jvcoach23

    jvcoach23 Guest

    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
    jvcoach23, Apr 29, 2010
    #3
  4. jvcoach23

    jvcoach23 Guest

    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
    jvcoach23, Apr 29, 2010
    #4
  5. jvcoach23

    jvcoach23 Guest

    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.
    jvcoach23, Apr 30, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ivan Demkovitch

    Cookie vs HttpCookie

    Ivan Demkovitch, Dec 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    5,008
    bruce barker
    Dec 8, 2003
  2. Chris
    Replies:
    0
    Views:
    685
    Chris
    Dec 9, 2003
  3. Waqqas Farooq

    complex data structure in httpcookie

    Waqqas Farooq, Jun 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    344
    avnrao
    Jun 30, 2004
  4. Gawel
    Replies:
    1
    Views:
    5,563
    Scott Allen
    Feb 21, 2005
  5. Replies:
    5
    Views:
    26,584
    Mike Schilling
    Mar 29, 2006
Loading...

Share This Page