Re: Need Help with cookies
- From: "Phillip N Rounds" <prounds@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 21 Dec 2005 12:19:15 -0500
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" <prounds@xxxxxxxxxxxxxxxxxx> wrote in message
news:%233xt5caBGHA.2300@xxxxxxxxxxxxxxxxxxxxxxx
> 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 ;
> }
> }
>
>
>
>
.
- Follow-Ups:
- Re: Need Help with cookies
- From: clintonG
- Re: Need Help with cookies
- References:
- Need Help with cookies
- From: Phillip N Rounds
- Need Help with cookies
- Prev by Date: Re: binary vs. xml serialization
- Next by Date: Can I add text after the node link in a treeview/sitemap?
- Previous by thread: Re: Need Help with cookies
- Next by thread: Re: Need Help with cookies
- Index(es):
Relevant Pages
|