쿠키

2012-07-10 4 views
0
내가 서버에 쿠키를 추가 해요

추가하지 않습니다 : - : 내가 요청에서 쿠키를 읽을 수 있어요 때쿠키

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); 
} 

을하지만 cookie.Expire는 날짜 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; 
    } 
} 

로그를 동일 쿠키. 이름 : 쿠키 쿠키. 값 : 0 쿠키. 확장 : 01.01.0001 0:00:00 나는이 문제를 이해하지 못한다.

+0

의 조립 using System.Web.Security를 추가 I 읽기 - 만료 = 01.01.0001 : ".. 그것을 바꿔주세요 .. 당신이 원하는 것을 이해하기가 어렵습니다 .. – Yasser

+0

나는이 튜토리얼이 대안으로 생각할 수 있음을 발견했다 : http://code-inside.de/blog -in/2010/10/19/howto-create-and-remove-cookies-asp-net-mvc/ – cpoDesign

+0

나는 s 다음과 같이이 문제를 해결했습니다. 단순히 쿠키의 값을 다시 설정하기 위해 orderId를 '0'으로 설정하십시오. 그러나 브라우저가 쿠키를 서버에서 제거한 후에 제거하지 않는 이유는 매우 흥미 롭습니다. – user571874

답변

1

기본적으로 두 가지 정보가 있어야합니다. 이드와 만료일입니다. 어떻게 별도의 쿠키에 만료 날짜를 저장에 대해 :

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); 
} 

은 그래서 당신이 쿠키 wmpaymentexpire의 값을 읽어 wmpayment 쿠키 날짜를 만료 확인합니다.

+0

나는 이것이 좋은 변형이라고 생각하지 않는다. 문제를 이해하고 싶습니다. – user571874

+1

내 제안이 마음에 들지 않으면 충분합니다. 그러나 Jonathan Rupp이 답변에서 쓴 것처럼 클라이언트는 쿠키의 만료 날짜를 서버로 되돌려 보내지 않습니다. 따라서 서버에서 쿠키의 만료 날짜를 설정하고이를 클라이언트로 보낼 수 있습니다. 그러나 클라이언트는이를 다시 보내지 않으므로 서버에서 읽을 수는 없습니다. 클린트에서 서버로 쿠키의 이름과 값만 전송됩니다. – user1429080

+0

대단히 감사합니다. 나는 그것에 대해 알지 못했다. 글쎄, 클라이언트에 존재하는 서버에서 쿠키를 삭제 한 이유를 말해 줄 수 있습니까? – user571874

0

쿠키가 서버에 다시 제출되면 '만료'옵션이 포함되지 않으므로 exp이 채워지지 않으므로 기본값 인 DateTIme.MinValue가 그대로 유지됩니다. 따라서 DateTime.Now < exp은 절대로 사실이 아니므로 int.TryParse(cookie.Value, out id)이 실행되지 않으므로 ID는 기본값 인 0을 유지합니다.

대신을 시도해보십시오

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; 
    } 
} 

당신이 만료 된 쿠키의 서버 측을 확인 할 필요가 없습니다 - 만료 된 경우, 클라이언트를 전송하지 않습니다.

+0

webmoney 판매자에 대한 요청을 제출하기 전에 쿠키 주문 ID를 유지합니다. 지불 주문이 성공적이면 쿠키를 삭제하고, 그렇지 않으면 사용자가 지불 주문을 보류하고있는 메시지가있는 앵커를 표시합니다. 따라서 Expire 쿠키 – user571874

+0

@ user571874를 확인해야합니다. '만료'값이 중요하면 쿠키의 값으로 저장해야합니다 (동일한 쿠키로 인코딩되거나 @ user1429080에서 제안한 것과 같이 별도로 인코딩해야 함). 클라이언트는 쿠키 만료 정보를 다시 서버로 보내지 않고 이름과 값만을 서버로 보냅니다. –

1

당신은 쿠키를 만들기 위해이 코드를 사용할 수 있습니다

  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); 

* 참고 : *이이 라인에 문제가 ...입니다 "때보다 FormsAuthenticationTicket