Chrome losing cookies Chrome losing cookies google-chrome google-chrome

Chrome losing cookies


let me give you a solution. i have used the cookies for storing most of the values here and is very much working in all browsers and is stored for the particular mentioned time. for this i have used static classes to be accessible every where.

I have also encoded and decoded here. but you can store this by removing encoding and decoding and passing normal. Here's my code

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

If you are very particular about using HttpSecureCode then use this link for building up 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 these you can easily store values in cookie and fetch value whenever required

using these methods is as simple as

For Setting Cookie:

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

For Getting Cookie:

string currency= CookieStore.GetCookie("currency");