public static Guid CreateAnonymousCustomerGuid() { if (HttpContext.Current.Session != null) { HttpContext.Current.Session["isAuthorize"] = false; } CommonHelper.DeleteCookie(MCookieCollectionName); var customerId = Guid.NewGuid(); // Если база не доступна то тут проблема. while (CustomerService.ExistsCustomer(customerId)) { customerId = Guid.NewGuid(); } CommonHelper.SetCookieCollection(MCookieCollectionName, new NameValueCollection { { "cUserId", customerId.ToString() } }, new TimeSpan(7, 0, 0, 0)); return(customerId); }
private static Customer GetCurrentCustomer() { Customer customer = null; if (HttpContext.Current != null) { var cachedCustomer = HttpContext.Current.Items[CustomerContextKey] as Customer; if (cachedCustomer != null) { return(cachedCustomer); } if (IsDebug) { customer = new Customer { CustomerRole = Role.Administrator, IsVirtual = true }; } //registered user if (customer == null) { customer = AuthorizeService.GetAuthenticatedCustomer(); } //load guest customer if (customer == null) { var customerCookie = CommonHelper.GetCookie(CustomerCookieName); if (customerCookie != null && !String.IsNullOrEmpty(customerCookie.Value)) { Guid customerGuid; if (Guid.TryParse(customerCookie.Value, out customerGuid) && !CustomerService.ExistsCustomer(customerGuid)) { customer = new Customer { Id = customerGuid, CustomerRole = Role.Guest }; } } } //create guest if not exists if (customer == null) { var customerId = Guid.NewGuid(); while (CustomerService.ExistsCustomer(customerId)) { customerId = Guid.NewGuid(); } customer = new Customer { Id = customerId, CustomerRole = Role.Guest }; SetCustomerCookie(customer.Id); } HttpContext.Current.Items[CustomerContextKey] = customer; } return(customer); }