user571874
user571874

Reputation: 589

cookie does not adding

I'm adding cookie on the server:

private void AddCookie(int id)
{
    HttpCookie cookie = new HttpCookie("wmpayment");
    cookie.Value = id.ToString();
    cookie.Expires = DateTime.Now.AddDays(2);
    this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
}

but when I am read cookie from Request - cookie.Expire equals date 01.01.0001

public static int WMPendingOrder
{
    get
    {
        var cookie = HttpContext.Current.Request.Cookies["wmpayment"];
        int id = 0;
        DateTime exp;

        if (cookie != null)
        {
            DateTime.TryParse(cookie.Expires.ToString(), out exp);
            if (DateTime.Now < exp)
                int.TryParse(cookie.Value, out id);
        }
        return id;
    }
}

log: COOKIE.Name: wmpayment COOKIE.Value: 0 COOKIE.Expire: 01.01.0001 0:00:00 I am not understand what the problem.

Upvotes: 0

Views: 1640

Answers (3)

Subhankar
Subhankar

Reputation: 126

You can use this code to creating a cookie:

            FormsAuthenticationTicket tkt;
            string cookiestr;
            HttpCookie ck;

            tkt = new FormsAuthenticationTicket(1, UsrNm, DateTime.Now,
      DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "Issue Ticket");
            cookiestr = FormsAuthentication.Encrypt(tkt);

            ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
            if (chkPersistCookie.Checked)
                ck.Expires = tkt.Expiration;
            ck.Path = FormsAuthentication.FormsCookiePath;
            Response.Cookies.Add(ck);

            string strRedirect;
            strRedirect = Request["ReturnUrl"];
            if (strRedirect == null)
                strRedirect = "~/default.aspx";
            Response.Redirect(strRedirect, true);

*Note:*add the assembly using System.Web.Security for FormsAuthenticationTicket

Upvotes: 1

user1429080
user1429080

Reputation: 9166

So there are basically two pieces of information you need to persist. The id and an expiry date. How about storing the expiry date in a separate cookie:

private void AddCookie(int id) 
{ 
    HttpCookie cookie = new HttpCookie("wmpayment"); 
    cookie.Value = id.ToString(); 
    cookie.Expires = DateTime.Now.AddDays(2); 
    this.ControllerContext.HttpContext.Response.Cookies.Add(cookie); 

    HttpCookie cookie = new HttpCookie("wmpaymentexpire"); 
    cookie.Value = DateTime.Now.AddDays(2).ToString(); 
    cookie.Expires = DateTime.Now.AddDays(2); 
    this.ControllerContext.HttpContext.Response.Cookies.Add(cookie); 
}

So to check the expires date for cookie wmpayment you read the value of cookie wmpaymentexpire.

Upvotes: 1

Jonathan Rupp
Jonathan Rupp

Reputation: 15762

When cookies are submitted back to the server, they do not contain the 'Expires' option, so exp isn't being populated, so it keeps it's default value, DateTIme.MinValue. Because of that, DateTime.Now < exp is never true, so int.TryParse(cookie.Value, out id) never runs, so id keeps it's default value, 0.

Try this instead:

public static int WMPendingOrder
{
    get
    {
        var cookie = HttpContext.Current.Request.Cookies["wmpayment"];
        int id = 0;

        if (cookie != null)
        {
            int.TryParse(cookie.Value, out id);
        }
        return id;
    }
}

You don't need to check for expired cookies server-side - if they expire, the client won't send them.

Upvotes: 0

Related Questions