tags:

views:

28

answers:

2

Hello!

I want to keep some different data in one cookie file and write this class, and want to know - is this good? For example - user JS enable.When user open his first page on my site, i write to session his GMT time and write with this manager JS state. (GMT time is ajax request with js). And i want to keep some data in this cookie (up to 10 values). Have any advices or tips?

/// <summary>
/// CookiesSettings
/// </summary>
internal enum CookieSetting
{
    IsJsEnable = 1,
}

internal class CookieSettingValue
{
    public CookieSetting Type { get; set; }

    public string Value { get; set; }
}

/// <summary>
/// Cookies to long time of expire
/// </summary>
internal class CookieManager
{
    //User Public Settings
    private const string CookieValueName = "UPSettings";

    private string[] DelimeterValue = new string[1] { "#" };

    //cookie daat
    private List<CookieSettingValue> _data;

    public CookieManager()
    {
        _data = LoadFromCookies();
    }

    #region Save and load

    /// <summary>
    /// Load from cookie string value
    /// </summary>
    private List<CookieSettingValue> LoadFromCookies()
    {
        if (!CookieHelper.RequestCookies.Contains(CookieValueName))
            return new List<CookieSettingValue>();

        _data = new List<CookieSettingValue>();

        string data = CookieHelper.RequestCookies[CookieValueName].ToString();

        string[] dels = data.Split(DelimeterValue, StringSplitOptions.RemoveEmptyEntries);

        foreach (string delValue in dels)
        {
            int eqIndex = delValue.IndexOf("=");
            if (eqIndex == -1)
                continue;

            int cookieType = ValidationHelper.GetInteger(delValue.Substring(0, eqIndex), 0);
            if (!Enum.IsDefined(typeof(CookieSetting), cookieType))
                continue;

            CookieSettingValue value = new CookieSettingValue();
            value.Type = (CookieSetting)cookieType;
            value.Value = delValue.Substring(eqIndex + 1, delValue.Length - eqIndex-1);
            _data.Add(value);
        }

        return _data;
    }

    public void Save()
    {
        CookieHelper.SetValue(CookieValueName, ToCookie(), DateTime.UtcNow.AddMonths(6));
    }

    #endregion

    #region Get value

    public bool Bool(CookieSetting type, bool defaultValue)
    {
        CookieSettingValue inList = _data.SingleOrDefault(x => x.Type == type);
        if (inList == null)
            return defaultValue;

        return ValidationHelper.GetBoolean(inList.Value, defaultValue);
    }

    #endregion

    #region Set value

    public void SetValue(CookieSetting type, int value)
    {
        CookieSettingValue inList = _data.SingleOrDefault(x => x.Type == type);

        if (inList == null)
        {
            inList = new CookieSettingValue();
            inList.Type = type;
            inList.Value = value.ToString();

            _data.Add(inList);
        }
        else
        {
            inList.Value = value.ToString();
        }
    }

    public void SetValue(CookieSetting type, bool value)
    {
        CookieSettingValue inList = _data.SingleOrDefault(x => x.Type == type);

        if (inList == null)
        {
            inList = new CookieSettingValue();
            inList.Type = type;
            inList.Value = value.ToString();

            _data.Add(inList);
        }
        else
        {
            inList.Value = value.ToString();
        }
    }

    #endregion

    #region Private methods

    private string ToCookie()
    {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < _data.Count; i++)
        {
            sb.Append((int)_data[i].Type);
            sb.Append("=");
            sb.Append(_data[i].Value);
            sb.Append(DelimeterValue[0]);
        }

        return sb.ToString();
    }

    /// <summary>
    /// Cookie length in bytes. Max - 4 bytes
    /// </summary>
    /// <returns></returns>
    private int GetLength()
    {
        return System.Text.Encoding.UTF8.GetByteCount(ToCookie());
    }

    #endregion
}

P.S. i want to keep many data in one cookies file to compress data and decrease cookies count.

+1  A: 

Don't put data into cookies. All cookie data is uploaded from the client on every request to your web site. Even users with good broadband connections often have very limited upload bandwidth, and so storing significant data in cookies can be very bad for perceived performance.

Instead, simply store a value in the cookie that you can use as a lookup to a database table when needed.

Joel Coehoorn
A: 

Don't put data into cookie. What Joel say is stands and I like to say one more think. Browser some time behave strange if you have a large amount data on your cookie, and you get problems that you do not even imaging where they come from. And this is from my experience.

behave strange:They sow blank white pages, or they can not load the page and you see the cursor wait and wait, or lose the cookie, or you lose data from your cookie and you can not login for example, and other thinks like that.

Aristos