Cookie not found in Request.Cookies collection

G

Guest

Hi,

I have one problem with cookies in ASP.NET application.

It seems that I can not retreive cookie from Request.Cookies collection.
I put cookie in Response.Cookies collection, and after page post back, when
I try to retreive it from Request.Cookies collection, it appears that it does
not exists.
This problem does not occur on several developing machines we use for
developing application, but occurs in another environment (another firm)
where application is being test.

What could be the problem? Is it possible that some firewall, web server
setting or something else could affect this?

Btw, cookies are enabled in user's browsers and web.config file is the same.

Thanks in advance
 
B

Brock Allen

Either 1) The browser has disabled cookies, or 2) perhaps there's a bug in
your code. Check #1, and if that's not it, then post the code for #2 :)
 
G

Guest

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
 
B

Brock Allen

Woah, that's a lot of code just to set a cookie. I'd suggest getting rid
of all of the layers and making the code much easier so you can test and
diagnose this problem you're having. Do this:

Sub Page_Load()
If Not IsPostBack Then
Dim c as New HttpCookie("Name")
c.Value = "Value"
Response.Cookies.Add(c)
Else
Dim c as HttpCookie = Request.Cookies("Name")
if c Is Nothing Then
Throw New ApplicationException("Ok, something's wrong")
End If
End If
End Sub

To emit a cookie you don't have to grab it from the Request.Cookies collection
-- always 'new' one up. It's not going to hurt to do that.
 
K

kareemmostafa

Since you mentioned that this was working on your Intranet, maybe you
should check the IE's security settings and lower it or trust this
other server...

Kareem Mostafa
 
G

Guest

Problem is solved.
Nothing was wrong with the code but their intranet settings.

Still, thanks for your help.
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top