473,386 Members | 1,699 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,386 software developers and data experts.

Cookies are killing me

I am having a difficult time with cookies. In my code, I need to allow a user to login to my admin site. I figure I can set a cookie, then authenticate them, and all should be well. This works, however, when I want to log off the user, my cookie is giving my issues with the expiration date. The expiration date is always "1/1/1," so I cannot delete the cookie.

The code below is my login logic. When I login a user, I create them a cookie as persistant, then use FormsAuthentication.SetAuthCookie, then I redirect them to the main admin page. When I debug the code, I can see that the cookie does have a sensible expiration date, but after the Response.Redirect, the sensible expiration date becomes "1/1/1." This is killing me. Any thoughts?

Thanks.
HttpCookie cookie = new HttpCookie("MustSeeHomesAdmin");
cookie.Expires = System.DateTime.Now.AddDays(7);
cookie.Values["Persistent"] = "true";
cookie.Value = contact.UserPboID.ToString();
HttpContext.Current.Response.Cookies.Add(cookie);

// Authenticate the user
FormsAuthentication.SetAuthCookie( contact.UserPboID.ToString(),true);
// Redirect browser back to originating page
Response.Redirect( Request.UrlReferrer.AbsolutePath + "?mod=homes" );
Nov 18 '05 #1
2 1317
Alex,

Your problem is that you are redirecting from your request. If you redirect
cookies are not set. You will need to either post back to the page before
redirecting or use some sort of intermediate page as to guarantee that the
Cookie (or Session Cookie for that matter) gets written out.

For situations like this I usually use an Intermediate Page callable
directly from a worker page:

http://www.west-wind.com/presentatio...ageDisplay.asp

From this you can re-direct AFTER the cookie was created.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web
"news.microsoft.com" <Al**@donotemailme.com> wrote in message
news:Ol**************@TK2MSFTNGP11.phx.gbl...
I am having a difficult time with cookies. In my code, I need to allow a
user to login to my admin site. I figure I can set a cookie, then
authenticate them, and all should be well. This works, however, when I want
to log off the user, my cookie is giving my issues with the expiration date.
The expiration date is always "1/1/1," so I cannot delete the cookie.

The code below is my login logic. When I login a user, I create them a
cookie as persistant, then use FormsAuthentication.SetAuthCookie, then I
redirect them to the main admin page. When I debug the code, I can see that
the cookie does have a sensible expiration date, but after the
Response.Redirect, the sensible expiration date becomes "1/1/1." This is
killing me. Any thoughts?

Thanks.
HttpCookie cookie = new HttpCookie("MustSeeHomesAdmin");
cookie.Expires = System.DateTime.Now.AddDays(7);
cookie.Values["Persistent"] = "true";
cookie.Value = contact.UserPboID.ToString();
HttpContext.Current.Response.Cookies.Add(cookie);

// Authenticate the user
FormsAuthentication.SetAuthCookie( contact.UserPboID.ToString(),true);
// Redirect browser back to originating page
Response.Redirect( Request.UrlReferrer.AbsolutePath + "?mod=homes" );
Nov 18 '05 #2
As a work around, you may try using Request.Cookies.Remove("NameOfCookie");
as opposed to setting the expires as well. I will look into your method to
see if I can get it working. Thanks for the help.

Alex
"Rick Strahl [MVP]" <ri********@hotmail.com> wrote in message
news:%2*****************@TK2MSFTNGP10.phx.gbl...
Alex,

Your problem is that you are redirecting from your request. If you redirect cookies are not set. You will need to either post back to the page before
redirecting or use some sort of intermediate page as to guarantee that the
Cookie (or Session Cookie for that matter) gets written out.

For situations like this I usually use an Intermediate Page callable
directly from a worker page:

http://www.west-wind.com/presentatio...ageDisplay.asp
From this you can re-direct AFTER the cookie was created.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web
"news.microsoft.com" <Al**@donotemailme.com> wrote in message
news:Ol**************@TK2MSFTNGP11.phx.gbl...
I am having a difficult time with cookies. In my code, I need to allow a
user to login to my admin site. I figure I can set a cookie, then
authenticate them, and all should be well. This works, however, when I want to log off the user, my cookie is giving my issues with the expiration date. The expiration date is always "1/1/1," so I cannot delete the cookie.

The code below is my login logic. When I login a user, I create them a
cookie as persistant, then use FormsAuthentication.SetAuthCookie, then I
redirect them to the main admin page. When I debug the code, I can see that the cookie does have a sensible expiration date, but after the
Response.Redirect, the sensible expiration date becomes "1/1/1." This is
killing me. Any thoughts?

Thanks.
HttpCookie cookie = new HttpCookie("MustSeeHomesAdmin");
cookie.Expires = System.DateTime.Now.AddDays(7);
cookie.Values["Persistent"] = "true";
cookie.Value = contact.UserPboID.ToString();
HttpContext.Current.Response.Cookies.Add(cookie);

// Authenticate the user
FormsAuthentication.SetAuthCookie( contact.UserPboID.ToString(),true);
// Redirect browser back to originating page
Response.Redirect( Request.UrlReferrer.AbsolutePath + "?mod=homes" );

Nov 18 '05 #3

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

Similar topics

4
by: Brian Burgess | last post by:
Hi all, Anyone know of any special issues with storing cookies with ASP? I'm trying this with two browsers: One is IE 6.0 with cookies set to 'prompt'. This has been working properly as any...
20
by: Brian Burgess | last post by:
Hi all, Anyone know if this is possible? If so, on which page would the cookie be? .. On the page calling a function defined in the include file? thanks in advance.. -BB
6
by: Colin Steadman | last post by:
I have created a function to kill all session variables that aren't in a safe list. This is the function - Sub PurgeSessionVariables For Each Item In Session.Contents Select Case Trim(Item)...
0
by: Dan Meehan | last post by:
I created a music website that allows people to sign up and post messages on some message boards and update their profiles and such. The login script uses cookies, so when they Log Into the site it...
6
by: Mark | last post by:
Hi... I've come across some weird bug with Response.Cookies. Or maybe it will be called "by design" but for the life of me I can't figure out what purpose it would serve. If you're setting a...
1
by: krose | last post by:
Hi Using a couple of examples on CodeProject.com I've create a nice little shell extension (adds some custom menus for proprietary file types). Everything is working fine, except I can't get it...
33
by: Todd | last post by:
OK, I created a .htm page within a new Web solution: -------------------------------------------------------- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head>...
6
by: Stephane | last post by:
Hi, I have a login page where if the user wants his access codes to be saved are set into a cookie. In the logout page, I want to delete those cookies. I tried this and this is not working at...
2
by: Ed Jay | last post by:
I have an application that sets a variable number of randomly-named cookies. I'm currently using the following script to kill all of them. I'm using the script because I couldn't find a simpler...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...

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.