By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

Cookie not found in Request.Cookies collection

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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 :)

-Brock
DevelopMentor
http://staff.develop.com/ballen
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


Nov 19 '05 #2

P: n/a
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

Nov 19 '05 #3

P: n/a
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen
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


Nov 19 '05 #4

P: n/a
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

Nov 19 '05 #5

P: n/a

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

Still, thanks for your help.
Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.