asp.net cookie domain problem solved

Discussion in 'ASP .Net' started by idog, Nov 5, 2003.

  1. idog

    idog Guest

    i noticed weird cookie problems when clients sometimes visit my site with
    the www. prefix and sometimes without the prefix. I decided to start
    setting the .domain property, but that doesnt work for localhost. So here
    is my solution hopefully it saves someone some grief. Let me know if I
    missed something obvious:

    Response.Cookies.Add(WriteCookie("testUID", TextBox1.Text,
    Request.Url.Host))


    Public Function WriteCookie(ByVal strKey As String, ByVal strValue As
    String, ByVal strHost As String) As HttpCookie

    Dim cook As New HttpCookie(strKey, strValue)

    cook.Expires = Now.AddYears(1)

    If strHost <> "localhost" Then

    Dim myArray As String()

    myArray = strHost.Split(".")

    If myArray.Length > 2 Then

    'we only want the last two segments of the host name

    strHost = myArray(myArray.Length - 2) & "." & myArray(myArray.Length - 1)

    End If

    cook.Domain = strHost

    End If

    WriteCookie = cook

    End Function



    Public Function DeleteCookie(ByVal strKey As String, ByVal strHost As
    String) As HttpCookie

    Dim cook As New HttpCookie(strKey)

    cook.Expires = Now.AddYears(-30)

    If strHost <> "localhost" Then

    Dim myArray As String()

    myArray = strHost.Split(".")

    If myArray.Length > 2 Then

    'we only want the last two segments of the host name

    strHost = myArray(myArray.Length - 2) & "." & myArray(myArray.Length - 1)

    End If

    cook.Domain = strHost

    End If

    DeleteCookie = cook

    End Function
     
    idog, Nov 5, 2003
    #1
    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. Scott Meddows
    Replies:
    1
    Views:
    409
    John Saunders
    Jun 8, 2004
  2. =?Utf-8?B?UGF1bA==?=

    Cookie Question (IP as domain and cookie file location)

    =?Utf-8?B?UGF1bA==?=, Jan 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,516
    Bruce Barker
    Jan 10, 2006
  3. =?Utf-8?B?TnVubw==?=

    Convert a PHP cookie to an ASP.NET cookie

    =?Utf-8?B?TnVubw==?=, Jan 31, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    456
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 31, 2006
  4. Michael Bacarella
    Replies:
    26
    Views:
    1,408
    harri
    Nov 20, 2007
  5. iotaivy
    Replies:
    0
    Views:
    157
    iotaivy
    Nov 10, 2005
Loading...

Share This Page