P
Phillip N Rounds
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 ;
}
}
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 ;
}
}