asp.net cookie domain problem solved

I

idog

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top