473,513 Members | 3,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Help with cookies

I'm having trouble with using cookies to monitor the stages of login.
I have a two stage Registration page ( register.aspx ) and my target page
( MyPage.aspx )
I'm using a cookie named LoginStatus to tract the stage of the login
process.
LoginStatus = "1" denotes that the first part of the login process has been
performed.
LoginStatus = "2" denotes that the login process has been completed.
The entire process works as ( I ) expected when I never close the browser.
Once I close the browser, the cookie I have been using is lost. I think the
problem is trying to alter an existing cookie, but that doesn't make sense
to me. Where's the flaw?

Thanks

Phil Rounds
Pseudo Code is as follows:

MyPage.aspx

private void Page_Load( object sender, EventArgs e)
{
System.Web.HttpContext cont = System.Web.HttpContext.Current;
// Check to see if there is even an instance of the cookie LoginStatus
if ( cont.Request.Cookies["LoginStatus"] == null )
Page.Response.Redirect("Register.aspx"); // Cookie doesn't even
exist, so go to the Register Page
if ( cont.Request.Cookies["LoginStatus"].Value != "2")
Page.Response.Redirect("Register.aspx") // Login has started, but not
completed

do the rest of the stuff
}
Register.aspx

private void Page_Load( object sender, EventArgs e)
{
System.Web.HttpContext cont = System.Web.HttpContext.Current;
// Check to see if there is even an instance of the cookie LoginStatus
if ( cont.Request.Cookies["LoginStatus"] == null )
{ HttpCookie cook = new HttpCookie("LoginStatus", "0") ;
// Create the cookie
BeginLoginProcess();
// Do the first part of the login pocess
}
else
{
if ( cont.Request.Cookies["LoginStatus"].Value = "1")
{ CompleteLogin(); // This is a return to
Register.aspx, so complete the login process
Page.Response.Redirect("MyPage.aspx"); // Go to
MyPage, which is where you want to be
return; }

if ( cont.Request.Cookies["LoginStatus"].Value = "2")
Page.Response.Redirect("MyPage.aspx") ; // You're already logged in,
so you shouldn't be here
}
}

BeginLoginProcess()
{
Do a whole bunch of stuff. If everything is ok,
System.Web.HttpContext cont = System.Web.HttpContext.Current;
if ( cont.Request.Cookies["LoginStatus"] != null )
// It can't possibly be null from the above, but what the hey
{
HttpCookie cook = cont.Request.Cookies["LoginStatus"];
cook.Value = "1" ; // We've completed
part 1 of the login process
cont.Response.Cookies.Add( cook );
return ;
}
}

CompleteLogin()
{
Do someother stuff
System.Web.HttpContext cont = System.Web.HttpContext.Current;
if ( cont.Request.Cookies["LoginStatus"] != null )
// It can't possibly be null from the above, but what the hey
{
HttpCookie cook = cont.Request.Cookies["LoginStatus"];
cook.Value = "2" ; // We've completed
part 1 of the login process
cont.Response.Cookies.Add( cook );
return ;
}
}


Dec 20 '05 #1
3 1815
The flaw -- if I can say so -- is using cookies for what a Session variable
is best used for. Just destroy the variable when through using it.

Furthermore, if you use a MultiView or a Wizard control for multi-step
processes the state is managed for you noting I'm talking about 2.0 in this
regard.

If you insist on using cookies on the basis of your comment I would say you
need to learn to "persist" the cookie.

search: c# persistent cookie

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
"Phillip N Rounds" <pr*****@cassandragroup.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I'm having trouble with using cookies to monitor the stages of login.
I have a two stage Registration page ( register.aspx ) and my target page
( MyPage.aspx )
I'm using a cookie named LoginStatus to tract the stage of the login
process.
LoginStatus = "1" denotes that the first part of the login process has
been performed.
LoginStatus = "2" denotes that the login process has been completed.
The entire process works as ( I ) expected when I never close the browser.
Once I close the browser, the cookie I have been using is lost. I think
the problem is trying to alter an existing cookie, but that doesn't make
sense to me. Where's the flaw?

Thanks

Phil Rounds
Pseudo Code is as follows:

MyPage.aspx

private void Page_Load( object sender, EventArgs e)
{
System.Web.HttpContext cont = System.Web.HttpContext.Current;
// Check to see if there is even an instance of the cookie LoginStatus
if ( cont.Request.Cookies["LoginStatus"] == null )
Page.Response.Redirect("Register.aspx"); // Cookie doesn't even
exist, so go to the Register Page
if ( cont.Request.Cookies["LoginStatus"].Value != "2")
Page.Response.Redirect("Register.aspx") // Login has started, but not
completed

do the rest of the stuff
}
Register.aspx

private void Page_Load( object sender, EventArgs e)
{
System.Web.HttpContext cont = System.Web.HttpContext.Current;
// Check to see if there is even an instance of the cookie LoginStatus
if ( cont.Request.Cookies["LoginStatus"] == null )
{ HttpCookie cook = new HttpCookie("LoginStatus", "0") ; //
Create the cookie
BeginLoginProcess(); // Do the first part of the login
pocess
}
else
{
if ( cont.Request.Cookies["LoginStatus"].Value = "1")
{ CompleteLogin(); // This is a return to
Register.aspx, so complete the login process
Page.Response.Redirect("MyPage.aspx"); // Go to
MyPage, which is where you want to be
return; }

if ( cont.Request.Cookies["LoginStatus"].Value = "2")
Page.Response.Redirect("MyPage.aspx") ; // You're already logged
in, so you shouldn't be here
}
}

BeginLoginProcess()
{
Do a whole bunch of stuff. If everything is ok,
System.Web.HttpContext cont = System.Web.HttpContext.Current;
if ( cont.Request.Cookies["LoginStatus"] != null ) // It
can't possibly be null from the above, but what the hey
{
HttpCookie cook = cont.Request.Cookies["LoginStatus"];
cook.Value = "1" ; // We've completed
part 1 of the login process
cont.Response.Cookies.Add( cook );
return ;
}
}

CompleteLogin()
{
Do someother stuff
System.Web.HttpContext cont = System.Web.HttpContext.Current;
if ( cont.Request.Cookies["LoginStatus"] != null ) // It
can't possibly be null from the above, but what the hey
{
HttpCookie cook = cont.Request.Cookies["LoginStatus"];
cook.Value = "2" ; // We've completed
part 1 of the login process
cont.Response.Cookies.Add( cook );
return ;
}
}

Dec 21 '05 #2
To any who are interested, the answer is that one has to re-set the
expiration of the cookie when modifying it's value.
This works:

HttpContext cont;
HttpCookie cookie = cont.Request.Cookies["LoginStatus"];
cookie.Value = "New Value";
cookie.Expires = DateTime.Now.AddYear(1);
cont.Response.Cookies.Add( Cookie );

If you don't change cookie.Expires, it expires at the end of the session.

This leads to another question I had, which is about the expiration date.
In the above example, the cookie should have an expiration date of one year
from now. When I read the cookie & look at the expiration date, it is
1/1/0001. Is this the default for a Session Cookie, or for a cookie which
doesn't expire? How do I get/set or view the real expiration date?

Thanks
"Phillip N Rounds" <pr*****@cassandragroup.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I'm having trouble with using cookies to monitor the stages of login.
I have a two stage Registration page ( register.aspx ) and my target page
( MyPage.aspx )
I'm using a cookie named LoginStatus to tract the stage of the login
process.
LoginStatus = "1" denotes that the first part of the login process has
been performed.
LoginStatus = "2" denotes that the login process has been completed.
The entire process works as ( I ) expected when I never close the browser.
Once I close the browser, the cookie I have been using is lost. I think
the problem is trying to alter an existing cookie, but that doesn't make
sense to me. Where's the flaw?

Thanks

Phil Rounds
Pseudo Code is as follows:

MyPage.aspx

private void Page_Load( object sender, EventArgs e)
{
System.Web.HttpContext cont = System.Web.HttpContext.Current;
// Check to see if there is even an instance of the cookie LoginStatus
if ( cont.Request.Cookies["LoginStatus"] == null )
Page.Response.Redirect("Register.aspx"); // Cookie doesn't even
exist, so go to the Register Page
if ( cont.Request.Cookies["LoginStatus"].Value != "2")
Page.Response.Redirect("Register.aspx") // Login has started, but not
completed

do the rest of the stuff
}
Register.aspx

private void Page_Load( object sender, EventArgs e)
{
System.Web.HttpContext cont = System.Web.HttpContext.Current;
// Check to see if there is even an instance of the cookie LoginStatus
if ( cont.Request.Cookies["LoginStatus"] == null )
{ HttpCookie cook = new HttpCookie("LoginStatus", "0") ; //
Create the cookie
BeginLoginProcess(); // Do the first part of the login
pocess
}
else
{
if ( cont.Request.Cookies["LoginStatus"].Value = "1")
{ CompleteLogin(); // This is a return to
Register.aspx, so complete the login process
Page.Response.Redirect("MyPage.aspx"); // Go to
MyPage, which is where you want to be
return; }

if ( cont.Request.Cookies["LoginStatus"].Value = "2")
Page.Response.Redirect("MyPage.aspx") ; // You're already logged
in, so you shouldn't be here
}
}

BeginLoginProcess()
{
Do a whole bunch of stuff. If everything is ok,
System.Web.HttpContext cont = System.Web.HttpContext.Current;
if ( cont.Request.Cookies["LoginStatus"] != null ) // It
can't possibly be null from the above, but what the hey
{
HttpCookie cook = cont.Request.Cookies["LoginStatus"];
cook.Value = "1" ; // We've completed
part 1 of the login process
cont.Response.Cookies.Add( cook );
return ;
}
}

CompleteLogin()
{
Do someother stuff
System.Web.HttpContext cont = System.Web.HttpContext.Current;
if ( cont.Request.Cookies["LoginStatus"] != null ) // It
can't possibly be null from the above, but what the hey
{
HttpCookie cook = cont.Request.Cookies["LoginStatus"];
cook.Value = "2" ; // We've completed
part 1 of the login process
cont.Response.Cookies.Add( cook );
return ;
}
}

Dec 21 '05 #3
<snip />

That's called a persistent cookie as I tried to explain and as I recall it
seems you never set an expiration in the first place so how can you be
"resetting" something that didn't exist?

And another thing you mistakenly assumed, a persistent cookie does not need
a date of expiration set a year into the future as you stated. Any values
greater than Now( ) is all that is needed to persist the cookie for that
period of time.

You set that value correctly in the code you show but the wierd date into
the past is not a default for persistent cookies. Setting the date into the
past deletes a cookie.

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
Dec 21 '05 #4

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

Similar topics

0
1511
by: collie | last post by:
Hi, I have an asp page that needs to create 2 different cookies: one for the admin and one for the user. The code that I have to work with was created by someone else. the page first requires to login and if username and password match then the user is redirected to the same page where he sees details of his message. What I have to do is...
1
1537
by: Karl | last post by:
Hi there! I have a big Problem with some Javascript: On a website, i want to paste some "confirm", if the visitor's browserlanguage is not "de". when he confirm, he'll be redirected to the english-translation of this page. to store this information, because it's ugly to ask on every single visit, if he want to switch to the translation.
3
45942
by: news.rcn.com | last post by:
How can I access the request and response object for a page using javascript. I want to stick some data on with something like request.setAttribute( "User's choice for later use" ). I can't seem to find a reference to request or response objects in JavaScript either in Google Groups or O'Reilly's "JavaScript the Definitive Guide" (surely it...
6
8836
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 all: if (Request.Cookies != null && Request.Cookies != null) { Response.Cookies.Value = null;
6
1574
by: Pete Davis | last post by:
I've never done anything with cookies. What I'm trying to do is very straight-forward, but for some reason, it just doesn't seem to want to work. I have a helper class with some static methods. Two are for setting and getting the name of the user to/from a cookie: public static string GetSiteUser(HttpRequest request) { HttpCookie...
2
4762
by: William | last post by:
The script below runs correctly in ASP but not ASPX. Im not sure why? Please help. Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: BC30081: 'If' must end with a...
4
4033
by: William | last post by:
Does anyone know what is causing the error below. The following scipt runs in .asp but not .aspx??? Please help. Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message:...
3
7591
by: Noremac | last post by:
My google skills must be dwindling. I am trying to determine how in ASP.NET 2.0 I can get the ReturnUrl querystring variable in Forms Authentication to contain the absolute url. Just like others that have posed this question, we are an enterprise environment that has multiple websites across multiple servers and we are trying to setup Web...
2
1867
bmallett
by: bmallett | last post by:
I am getting the following error: Error Type: ADODB.Command (0x800A0BB9) Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. /dcme/newframe/verify.asp, line 48 Line 48 is:
0
7269
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7177
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...
1
7123
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...
0
7542
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3248
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
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
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
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.