473,406 Members | 2,369 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,406 software developers and data experts.

Weird Cookie Behaviour

I am trying to perform site login using values from a cookie, but for some
weird reason the cookie is being destroyed after the browser window is
closed. I checked the browser settings for the cookies, and there is nothing
there that terminates cookies uopn browser close. The following is a sample
of the code I am runinng:

To write the cookie:
This code runs if the user is not logged on the site and the cookie does not
exist.

writeCookie(string user, string password)
{
try
{
HttpCookie loginCookie = new HttpCookie(cookieName);
Response.Cookies.Clear();
Response.Cookies.Add(loginCookie);
loginCookie.Values.Add(cookieName, user + " " + password);
}
catch
{
}
}

The code to retrieve the cookie does so by getting the value of the cookie
and login on the user. I get the value of the cookie this way:

cookieValue = Request.Cookies[cookieName].Value;

The whole t hing is that this code is on the onLoad event of the form. It's
supposed to get the values of the cookie, but I get a null reference
exception because the cookie does not exist even though I wrote it with the
code above.
Can anyone help?

HS
Nov 19 '05 #1
1 1679
I believe you need to set the expiration date of the cookie:

loginCookie.Expires = DateTime.Now.AddDays(x);
(or something like that)

Without the expiration date, I believe the cookie becomes a "session" cookie
and is destroyed when the browser exits.

Also, if you write the cookie, then redirect the user on the same page, some
browsers will not accept the cookie. (Older netscapes I believe)

HTH

Steve

"hecsan07" <he******@hotmail.com> wrote in message
news:45**********************************@microsof t.com...
I am trying to perform site login using values from a cookie, but for some
weird reason the cookie is being destroyed after the browser window is
closed. I checked the browser settings for the cookies, and there is nothing there that terminates cookies uopn browser close. The following is a sample of the code I am runinng:

To write the cookie:
This code runs if the user is not logged on the site and the cookie does not exist.

writeCookie(string user, string password)
{
try
{
HttpCookie loginCookie = new HttpCookie(cookieName);
Response.Cookies.Clear();
Response.Cookies.Add(loginCookie);
loginCookie.Values.Add(cookieName, user + " " + password);
}
catch
{
}
}

The code to retrieve the cookie does so by getting the value of the cookie
and login on the user. I get the value of the cookie this way:

cookieValue = Request.Cookies[cookieName].Value;

The whole t hing is that this code is on the onLoad event of the form. It's supposed to get the values of the cookie, but I get a null reference
exception because the cookie does not exist even though I wrote it with the code above.
Can anyone help?

HS

Nov 19 '05 #2

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

Similar topics

1
by: Ben Hearsum | last post by:
I'm confused about the way Cookie.load outputs cookies. Is there a way I can tell it to give me the value of a key? I set my cookie like this: mycookie = Cookie.SimpleCookie() mycookie =...
6
by: Ajay | last post by:
hi! i am printing a simple cookie, but instead of printing um=name:blah&access:admin&exp:2312390.909 its printing um="name:blah&access:admin&exp:2312390.909" why the quotes?
9
by: Marco Krechting | last post by:
Hi All, I have a page with a list of hyperlinks. I want to save information in a cookie about the fact that I entered an hyperlink or not. When I click one of the hyperlinks I want this stored...
1
by: Joe | last post by:
Hi folks, Been looking at this for the day so any ideas would be appreciated. Here's the scenario, bear with me - it looks more complicated than it is :) I have a page templating system that...
1
by: Strange Cat | last post by:
Hi everyone! I have a weird problem with FormsAuthentication. I have an app that works just fine with FormsAuthentication. The user requests the homepage, he is redirected to login page,...
1
by: dotCore | last post by:
Hi, I have VS2003.NET, XP Pro and SQL 2000. The .NET framework is 1.1 The problem I am having with the source code is that I cannot authenticate any user. The sproc returns the user name from...
2
by: anathema | last post by:
this is not specifically behaviour related, but i thought i would risk asking anyway. i am working on a script that triggers a blind down or blind up effect by clicking on a particular link. it...
1
by: anathema | last post by:
Having a bit of trouble adapting some code for my use. This is the desired outcome - a user clicks on a#tools-link. This shows / hides the div #tool-box and a cookie is set to remember it's...
5
by: mosscliffe | last post by:
I have the following code, which I thought would create a cookie, if one did not exist and on the html form being sent to the server, it would be availabe for interrogation, when the script is run...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.