Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > asp.net cookie domain problem solved

Reply
Thread Tools

asp.net cookie domain problem solved

 
 
idog
Guest
Posts: n/a
 
      11-05-2003
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Populating a dictionary, fast [SOLVED SOLVED] Michael Bacarella Python 26 11-20-2007 03:02 PM



Advertisments