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 ;
}
} 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 ; } }
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 ; } }
<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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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.
|
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...
|
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;
|
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...
| |
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...
|
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:...
|
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...
|
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:
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |