473,382 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Cookie not found in Request.Cookies collection

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
5 5224
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
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
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
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

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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
I want to know what's the differences between session cookie and regular cookie. In ASP, when we create cookie, we do the following to identify an user: Response.Cookies("name") = value Is...
8
by: John Dalberg | last post by:
What happens when a cookie expires? Does it mean that when the browser or sessions ends, it doesn't get saved? I am using Opera and looking at available cookies and I can some cookies that have...
1
by: Jeff | last post by:
When is the value of an authentication cookie sent from the browser to the server - when the value is read from the Cookies collection? Or, is it possible that the Cookies collection is populated...
3
by: rss | last post by:
SUMMARY: ========== I am unable to pass along a simple Cookie obtained from a HttpWebRequest call (Machine A's ASP.NET app) so that another Web Server (Machine B) recongnizes the cookie. I...
2
by: James | last post by:
Help! I've made a cookie with a key called 'List' and subkey 'item3' and assigned it a value of "3" Response.Cookies("List")("item3")="3" How do I now delete the subkey "item3" without...
1
by: William | last post by:
ERROR: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. This if then statement only works if a cookie exists. It fails if the cookie...
1
by: Ken Varn | last post by:
I have a page that uses two cookies. On postback, both cookies are updated and added back into the Page.Response.Cookies collection. For some reason, only the first cookie is actually updated...
2
by: Owen | last post by:
I have a web app that is a mixture of ASP and ASP.NET pages. Largely the only data passed between them is via the querystring, or by reading from a database. However there is a requirement for...
1
by: Joe | last post by:
In ASP.NET 1.1 I could detected expired form authentication tickets (which closely coincide with my expired session) by checking for the Authentication Cookie when the login screen loads. If the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.