What Good way to keep some different data in Cookies in asp.net?

Posted by Dmitriy on Stack Overflow See other posts from Stack Overflow or by Dmitriy
Published on 2010-06-06T16:53:11Z Indexed on 2010/06/06 17:02 UTC
Read the original article Hit count: 223

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about mvc