views:

19

answers:

0

Hi,

I m working with a complexe nested cart items and i need to store my session cookies in the user profile to persist. I use one custom profile extender to store cookies but i don´t found the solution to make it persistent. I have tried using custom types serialized binaries with no sucess.

Thanks for your help.

source base from:link text

public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
    {
        string username = (string)context["UserName"];
        bool authenticated = (bool)context["IsAuthenticated"];
        SettingsPropertyValueCollection settings = new SettingsPropertyValueCollection();
        if (collection.Count == 0)
            return settings;
        foreach (SettingsProperty sp in collection)
        {
            settings.Add(new SettingsPropertyValue(sp));
        }
        string cookie = _cookieName + "_" + username;
        HttpCookie cookieObj = HttpContext.Current.Request.Cookies[cookie];
        if (cookieObj == null)
            return settings;
        string data = cookieObj["SerializedData"];
        BinaryFormatter formater = new BinaryFormatter();
        MemoryStream ms = new MemoryStream(Convert.FromBase64String(data));
        Hashtable table = formater.Deserialize(ms) as Hashtable;
        foreach (SettingsPropertyValue spv in settings)
        {
            spv.Deserialized = true;
            spv.PropertyValue = table[spv.Name];
        }
        ms.Close();
        return settings;
    }
    public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
    {
        string username = (string)context["UserName"];
        bool authenticated = (bool)context["IsAuthenticated"];
        if (String.IsNullOrEmpty(username) || collection.Count == 0)
            return;
        string cookie = _cookieName + "_" + username;
        HttpCookie cookieObj = HttpContext.Current.Request.Cookies[cookie];
        if (cookieObj == null)
            cookieObj = new HttpCookie(cookie);
        cookieObj.Expires = DateTime.Now.AddMinutes(CookieExpire);

        Hashtable table = new Hashtable();
        foreach (SettingsPropertyValue pv in collection)
        {
            if (!(bool)pv.Property.Attributes["AllowAnonymous"] && !authenticated)
            {
                continue;
            }
            table.Add(pv.Name, pv.PropertyValue);
        }
        BinaryFormatter formater = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        formater.Serialize(ms, table);
        string dataString = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
        ms.Close();
        cookieObj["SerializedData"] = dataString;
        HttpContext.Current.Response.Cookies.Add(cookieObj);
    }