Chrome will lose cookies

I get an error on my site that I don’t see in my Dev environment, and it seems to only happen with Chrome. I was looking a bit for a solution to this, and I only find problems with the Auth cookie. (In the past, I really raised the issue of chrome and the auth cookie), but that's different.

I store a basket of users in a cookie. I set the cookie this way

HttpCookie responseCookie = HttpContext.Response.Cookies[CartHelper.CART]; responseCookie.PackCartCookie(vm.Cart); 

If the PackCartCookie extension method sets a cookie this way

 cookie.Value = HttpUtility.UrlEncode(cookieValue); 

This is the result of saving a cookie with the following settings

  • Domain = www.foo.com
  • RawSize = 230b
  • Path = /
  • Expires = Session
  • HttpOnly = HttpOnly
  • Value = Encryption

When a user interacts with the site, it seems that the Cart Cookie is being created, but from time to time it is lost or deleted. When I look at the Elmah error and look at the HTTP_COOKIE, I can see all the other cookies (I have others set in the same way, which function is fine), but I do not see the basket cookie.

I had to change the code to be more secure due to this problem. But, as you can imagine, the cookie cookie is used during the purchase process, and I did not manage when I respond to the purchase, where I accept payment, but the system crashes when the cart is gone and the user does not receive a notification of a successful purchase. Fortunately, I caught these early and returned users.

User agents where I saw the problem

  • Mozilla / 5.0 (X11; Linux i686) AppleWebKit / 537.36 (KHTML, e.g. Gecko) Chrome / 29.0.1547.62 Safari / 537.36
  • Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.36 (KHTML, e.g. Gecko) Chrome / 29.0.1547.57 Safari / 537.36
  • Mozilla / 5.0 (Windows NT 6.0) AppleWebKit / 537.36 (KHTML, e.g. Gecko) Chrome / 29.0.1547.62 Safari / 537.36
+4
source share
1 answer

Let me give you a solution. I used cookies to store most of the values ​​here and it works a lot in all browsers and is stored for the specified time. for this, I used static classes to access anywhere.

I also coded and decrypted here. but you can save this by removing encoding and decoding and skipping normal. Here is my code

Here I put my class with static methods. I used HttpSecureCode with Encode and Decode using machine key cryptography. which may not be available by default in this case. you can directly put the value.

If you're particularly into using HttpSecureCode , use this to create your class.

 public class CookieStore { public static void SetCookie(string key, string value, TimeSpan expires) { HttpCookie encodedCookie = HttpSecureCookie.Encode(new HttpCookie(key, value)); if (HttpContext.Current.Request.Cookies[key] != null) { var cookieOld = HttpContext.Current.Request.Cookies[key]; cookieOld.Expires = DateTime.Now.Add(expires); cookieOld.Value = encodedCookie.Value; HttpContext.Current.Response.Cookies.Add(cookieOld); } else { encodedCookie.Expires = DateTime.Now.Add(expires); HttpContext.Current.Response.Cookies.Add(encodedCookie); } } public static string GetCookie(string key) { string value = string.Empty; HttpCookie cookie = HttpContext.Current.Request.Cookies[key]; if (cookie != null) { // For security purpose, we need to encrypt the value. HttpCookie decodedCookie = HttpSecureCookie.Decode(cookie); value = decodedCookie.Value; } return value; } } 

using them, you can easily save values ​​in a cookie and when you select a value

using these methods is as simple as

To set a cookie:

 CookieStore.SetCookie("currency", "GBP", TimeSpan.FromDays(1)); // here 1 is no of days for cookie to live 

To receive cookies:

 string currency= CookieStore.GetCookie("currency"); 
+2
source

Source: https://habr.com/ru/post/1500463/


All Articles