public static void SetValue(this CustomerContextData contextData, string alias, string value)
        {
            Ensure.ParameterNotNullOrEmpty(alias, "alias");
            Ensure.ParameterNotNullOrEmpty(value, "value");

            contextData.Values.Remove(contextData.Values.FirstOrDefault(x => x.Key == alias));
            contextData.Values.Add(new KeyValuePair <string, string>(alias, value));
        }
 public static string GetValue(this CustomerContextData contextData, string alias)
 {
     return(contextData.Values.FirstOrDefault(x => x.Key == alias).Value);
 }
Esempio n. 3
0
        /// <summary>
        /// Creates an anonymous customer
        /// </summary>
        private void CreateAnonymousCustomer()
        {
            var customer = _customerService.CreateAnonymousCustomerWithKey();
            CurrentCustomer = customer;
            ContextData = new CustomerContextData()
            {
                Key = customer.Key
            };

            CacheCustomer(customer);
        }
 /// <summary>
 /// Serializes the <see cref="CustomerContextData"/> to JSON
 /// </summary>
 /// <param name="contextData">
 /// The context data.
 /// </param>
 /// <returns>
 /// The <see cref="string"/>.
 /// </returns>
 public static string ToJson(this CustomerContextData contextData)
 {
     return(EncryptionHelper.Encrypt(JsonConvert.SerializeObject(contextData)));
 }
Esempio n. 5
0
        /// <summary>
        /// Initializes this class with default values
        /// </summary>
        private void Initialize()
        {
            // see if the key is already in the request cache
            var cachedContextData = _cache.RequestCache.GetCacheItem(CustomerCookieName);

            if (cachedContextData != null)
            {
                ContextData = (CustomerContextData)cachedContextData;
                var key = ContextData.Key;
                TryGetCustomer(key);
                return;
            }

            // retrieve the merchello consumer cookie
            var cookie = _umbracoContext.HttpContext.Request.Cookies[CustomerCookieName];

            if (cookie != null)
            {
                ContextData = cookie.ToCustomerContextData();

                try
                {
                    TryGetCustomer(ContextData.Key);
                }
                catch (Exception ex)
                {
                    LogHelper.Error<CustomerContext>("Decrypted guid did not parse", ex);
                    CreateAnonymousCustomer();
                }                             
            }
            else 
            {
                CreateAnonymousCustomer();
            } // a cookie was not found
        }