473,564 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(str ing user, string password)
{
try
{
HttpCookie loginCookie = new HttpCookie(cook ieName);
Response.Cookie s.Clear();
Response.Cookie s.Add(loginCook ie);
loginCookie.Val ues.Add(cookieN ame, 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 1692
I believe you need to set the expiration date of the cookie:

loginCookie.Exp ires = DateTime.Now.Ad dDays(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******@hotma il.com> wrote in message
news:45******** *************** ***********@mic rosoft.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(str ing user, string password)
{
try
{
HttpCookie loginCookie = new HttpCookie(cook ieName);
Response.Cookie s.Clear();
Response.Cookie s.Add(loginCook ie);
loginCookie.Val ues.Add(cookieN ame, 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
3576
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 = "foobar" print mycookie print("Content-Type: text/html\n\n")
6
2190
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
4474
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 in a cookie and a small bullit shown in front of the hyperlink, so when I reload the page I can immediately see which hyperlinks I already visited...
1
1531
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 inherits from the page class and adds a mixture of usercontrols and literals when requested. One of the usercontrols is a menu that expands on...
1
1540
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, then enters valid username and password that are checked versus a db table, if the info is ok i execute SetAuthCookie(xxx, False) then i open a new...
1
1129
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 database which also means that the user should be authenticated. It runs the SetAuthCookie method with success. The resulting page is no different...
2
1294
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 then remembers the status of that choice by setting a cookie. the technique was taken from here: ...
1
1546
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 status so that when the user goes to another page the #tool-box retains their choice. The show / hide part is fine. It is the cookie stuff I can not...
5
1446
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 a second time. It seems to me there is something basic missing in that I should need to tell the server I am sending a cookie, but all the docs I...
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.