Hi Brock,
As I already wrote, cookies are enabled in browser.
I doubt the code is the problem since it works in our intranet. I think
something in that other firm intranet causes this problem.
Anyway, code is little bit longer but here it is:
1. when button btnSetCookie is clicked, SetCookie() function is called:
Private Function SetCookie() As Boolean
Dim hcoCookie As HttpCookie
hcoCookie = GetTestCookie(COOKIE_TEST_NAME, Me)
SetTestCookieValue(COOKIE_TEST_NAME, COOKIE_TEST_KEY, _
"True", _
"True", Me)
End Function
2. GetTestCookie() function code:
Private Function GetTestCookie(ByVal CookieName As String, _
ByRef CurrentPage As Page) As HttpCookie
Dim hcoCookie As HttpCookie
Try
hcoCookie = Me.Request.Cookies(CookieName)
If hcoCookie Is Nothing Then
hcoCookie = New HttpCookie(CookieName)
hcoCookie.Expires = Now.AddYears(1)
'hcoCookie.Path = CurrentPage.Request.ApplicationPath
CurrentPage.Response.Cookies.Add(hcoCookie)
Else
hcoCookie.Expires = Now.AddYears(1)
'hcoCookie.Path = CurrentPage.Request.ApplicationPath
End If
Catch ex As Exception
hcoCookie = Nothing
End Try
Return hcoCookie
End Function
3. SetTestCookieValue() function code:
Private Sub SetTestCookieValue(ByVal CookieName As String, _
ByVal Key As String, ByVal Value As String, _
ByVal DefaultValue As String, ByRef CurrentPage As Page)
Dim hcoCookie As HttpCookie
Dim strKeyValue As String
Try
hcoCookie = GetTestCookie(CookieName, CurrentPage)
If Not hcoCookie Is Nothing Then
strKeyValue = hcoCookie.Values(Key)
If strKeyValue Is Nothing Then
hcoCookie.Values.Add(Key, DefaultValue)
Else
hcoCookie.Values.Set(Key, Value)
End If
CurrentPage.Response.Cookies.Set(hcoCookie)
End If
Catch ex As Exception
End Try
End Sub
4. After this button btnShowCookie is clicked, and ShowCookieValues()
function is called:
Private Sub ShowCookieValues()
Dim strCookieEnabled As String
Dim hcoCookie As HttpCookie
'direct approach to Cookies collection
hcoCookie = Request.Cookies(COOKIE_TEST_NAME)
If Not (hcoCookie Is Nothing) Then
'display cookie value on page
Else
lblCookieDetails.Text = "Cookie Not Set!"
End If
'indirect approach to Cookies collection through our functions
strCookieEnabled = TestCookies()
'cookie test result - display result on page
lblCookieValue.Text = strCookieEnabled
End Sub
Private Function TestCookies() As String
Dim hcoCookie As HttpCookie
Dim strKeyValue As String
hcoCookie = GetTestCookie(COOKIE_TEST_NAME, Me)
If Not hcoCookie Is Nothing Then
strKeyValue = GetTestCookieValue(hcoCookie, _
COOKIE_TEST_KEY, _
COOKIE_TEST_DEFAULT_VALUE, Me)
If Not strKeyValue Is Nothing Then
Return "ENABLED in our functions. Key value should be
""True"". Key value = " & strKeyValue
Else
Return "ENABLED in our functions, but key values are not.
Key value should be ""True"". Key value = " & strKeyValue
End If
Else
Return "DISABLED in our functions."
End If
End Function
Private Function GetTestCookieValue(ByVal Cookie As HttpCookie, _
ByVal Key As String, ByVal DefaultValue As String, _
ByRef CurrentPage As Page) As String
Dim strKeyValue As String
Try
strKeyValue = Cookie.Values(Key)
If strKeyValue Is Nothing Then
strKeyValue = DefaultValue
Cookie.Values.Add(Key, strKeyValue)
CurrentPage.Response.Cookies.Set(Cookie)
End If
Catch ex As Exception
Return Nothing
End Try
Return strKeyValue
End Function
COOKIE_TEST_NAME, COOKIE_TEST_KEY and similar, are constants.
I use 2 different approaches to retreive cookie values:
- hcoCookie = Request.Cookies(COOKIE_TEST_NAME) - direct approach - result
is "Cookie Not Set!"
- function TestCookies() - basic idea for it is: when cookie value is
requested, if there is no cookie, it is created and default value is set.
None of exceptions occurs - I checked that with error messages that are
displayed in Try/Catch block which I deleted to make code here shorter.
Thanks