/// <summary>
        /// Set the current customer by Middleware
        /// </summary>
        /// <returns></returns>
        public virtual async Task <Customer> SetCurrentCustomer()
        {
            Customer customer = null;

            //check whether request is made by a background (schedule) task
            if (_httpContextAccessor.HttpContext == null)
            {
                //in this case return built-in customer record for background task
                customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.BackgroundTask);

                //if customer comes from background task, doesn't need to create cookies
                if (customer != null)
                {
                    //cache the found customer
                    _cachedCustomer = customer;
                    return(customer);
                }
            }

            //set customer as a background task if method setted as AllowAnonymous
            var endpoint = _httpContextAccessor.HttpContext?.GetEndpoint();

            if (endpoint?.Metadata?.GetMetadata <IAllowAnonymous>() is object)
            {
                customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.BackgroundTask);
            }

            if (customer == null || customer.Deleted || !customer.Active)
            {
                //try to get registered user
                customer = await _authenticationService.GetAuthenticatedCustomer();
            }

            if (customer == null)
            {
                //try to get api user
                customer = await _apiauthenticationService.GetAuthenticatedCustomer();

                //if customer comes from api, doesn't need to create cookies
                if (customer != null)
                {
                    //cache the found customer
                    _cachedCustomer = customer;
                    return(customer);
                }
            }

            if (customer != null && !customer.Deleted && customer.Active)
            {
                //get impersonate user if required
                var impersonatedCustomerId = customer.GetAttributeFromEntity <string>(SystemCustomerAttributeNames.ImpersonatedCustomerId);
                if (!string.IsNullOrEmpty(impersonatedCustomerId))
                {
                    var impersonatedCustomer = await _customerService.GetCustomerById(impersonatedCustomerId);

                    if (impersonatedCustomer != null && !impersonatedCustomer.Deleted && impersonatedCustomer.Active)
                    {
                        //set impersonated customer
                        _originalCustomerIfImpersonated = customer;
                        customer = impersonatedCustomer;
                    }
                }
            }

            if (customer == null || customer.Deleted || !customer.Active)
            {
                //get guest customer
                var customerguid = await _authenticationService.GetCustomerGuid();

                if (!string.IsNullOrEmpty(customerguid))
                {
                    if (Guid.TryParse(customerguid, out Guid customerGuid))
                    {
                        //get customer from guid (should not be registered)
                        var customerByguid = await _customerService.GetCustomerByGuid(customerGuid);

                        if (customerByguid != null && !customerByguid.IsRegistered())
                        {
                            customer = customerByguid;
                        }
                    }
                }
            }

            if (customer == null || customer.Deleted || !customer.Active)
            {
                var crawler = _httpContextAccessor.HttpContext.Request?.Crawler();
                //check whether request is made by a search engine, in this case return built-in customer record for search engines
                if (crawler != null)
                {
                    customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.SearchEngine);
                }
            }

            if (customer == null || customer.Deleted || !customer.Active)
            {
                //create guest if not exists
                string referrer = _httpContextAccessor?.HttpContext?.Request?.Headers[HeaderNames.Referer];
                customer = await _customerService.InsertGuestCustomer(_storeContext.CurrentStore, referrer);
            }

            if (!customer.Deleted && customer.Active)
            {
                //set customer cookie
                await _authenticationService.SetCustomerGuid(customer.CustomerGuid);
            }
            //cache the found customer
            return(_cachedCustomer = customer ?? throw new Exception("No customer could be loaded"));
        }
Exemple #2
0
        /// <summary>
        /// Set the current customer by Middleware
        /// </summary>
        /// <returns></returns>
        public virtual async Task <Customer> SetCurrentCustomer()
        {
            Customer customer = null;

            //check whether request is made by a background (schedule) task
            if (_httpContextAccessor.HttpContext == null)
            {
                //in this case return built-in customer record for background task
                customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.BackgroundTask);

                //if customer comes from background task, doesn't need to create cookies
                if (customer != null)
                {
                    //cache the found customer
                    _cachedCustomer = customer;
                    return(customer);
                }
            }

            //set customer as a background task if method setted as AllowAnonymous
            var endpoint = _httpContextAccessor.HttpContext?.GetEndpoint();

            if (endpoint?.Metadata?.GetMetadata <IAllowAnonymous>() is object)
            {
                customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.Anonymous);

                //if customer comes from Anonymous method, doesn't need to create cookies
                if (customer != null)
                {
                    //cache the found customer
                    _cachedCustomer = customer;
                    return(customer);
                }
            }

            if (customer == null || customer.Deleted || !customer.Active)
            {
                //try to get registered user
                customer = await _authenticationService.GetAuthenticatedCustomer();

                //if customer is authenticated
                if (customer != null)
                {
                    //remove cookie
                    await _authenticationService.SetCustomerGuid(Guid.Empty);

                    //set if use impersonated session
                    var impersonatedCustomer = await SetImpersonatedCustomer(customer);

                    if (impersonatedCustomer != null)
                    {
                        customer = impersonatedCustomer;
                    }

                    //cache the found customer
                    _cachedCustomer = customer;


                    return(customer);
                }
            }

            if (customer == null)
            {
                //try to get api user
                customer = await _apiauthenticationService.GetAuthenticatedCustomer();

                //if customer comes from api, doesn't need to create cookies
                if (customer != null)
                {
                    //set if use impersonated session
                    var impersonatedCustomer = await SetImpersonatedCustomer(customer);

                    if (impersonatedCustomer != null)
                    {
                        customer = impersonatedCustomer;
                    }

                    //cache the found customer
                    _cachedCustomer = customer;
                    return(customer);
                }
            }

            if (customer == null || customer.Deleted || !customer.Active)
            {
                //get guest customer
                var customerguid = await _authenticationService.GetCustomerGuid();

                if (!string.IsNullOrEmpty(customerguid))
                {
                    if (Guid.TryParse(customerguid, out Guid customerGuid))
                    {
                        //get customer from guid (cannot not be registered)
                        var customerByguid = await _customerService.GetCustomerByGuid(customerGuid);

                        if (customerByguid != null && !await _groupService.IsRegistered(customerByguid))
                        {
                            customer = customerByguid;
                        }
                    }
                }
            }

            if (customer == null || customer.Deleted || !customer.Active)
            {
                var isCrawler = _detectionService.Crawler?.IsCrawler;
                //check whether request is made by a search engine, in this case return built-in customer record for search engines
                if (isCrawler.HasValue && isCrawler.Value)
                {
                    customer = await _customerService.GetCustomerBySystemName(SystemCustomerNames.SearchEngine);
                }
            }

            if (customer == null || customer.Deleted || !customer.Active)
            {
                //create guest if not exists
                customer = await _customerService.InsertGuestCustomer(CurrentStore);

                string referrer = _httpContextAccessor?.HttpContext?.Request?.Headers[HeaderNames.Referer];
                if (!string.IsNullOrEmpty(referrer))
                {
                    await _userFieldService.SaveField(customer, SystemCustomerFieldNames.UrlReferrer, referrer);
                }
                //set customer cookie
                await _authenticationService.SetCustomerGuid(customer.CustomerGuid);
            }

            //cache the found customer
            return(_cachedCustomer = customer ?? throw new Exception("No customer could be loaded"));
        }