Esempio n. 1
0
 public static CustomerViewModel ToModel(this RCustomer request)
 {
     if (request == null)
     {
         return(null);
     }
     return(new CustomerViewModel()
     {
         Email = request.Email,
         Id = request.Id,
         Status = request.Status,
         FullName = request.FullName,
         Type = request.Type,
         LanguageId = request.LanguageId,
         Code = request.Code,
         BirthdayValue = request.Birthday,
         CreatedDateUtc = request.CreatedDateUtc,
         CreatedUid = request.CreatedUid,
         EmailConfirmed = request.EmailConfirmed,
         FailedLoginAttempts = request.FailedLoginAttempts,
         Gender = request.Gender,
         IsTaxExempt = request.IsTaxExempt,
         LastIpAddress = request.LastIpAddress,
         PhoneNumber = request.PhoneNumber,
         PhoneNumberConfirmed = request.PhoneNumberConfirmed,
         ShardId = request.ShardId,
         StoreId = request.StoreId,
         TwoFactorEnabled = request.TwoFactorEnabled,
         UpdatedDateUtc = request.UpdatedDateUtc,
         UpdatedUid = request.UpdatedUid,
         Version = request.Version,
     });
 }
Esempio n. 2
0
        public async Task <ICommandResult> Handle(CustomerChangeCommand mesage)
        {
            try
            {
                ICommandResult result;
                RCustomer      customerFromDb = await _customerService.GetFromDb(mesage.Id);

                if (customerFromDb == null)
                {
                    result = new CommandResult()
                    {
                        Message      = "",
                        ObjectId     = "",
                        Status       = CommandResult.StatusEnum.Fail,
                        ResourceName = ResourceKey.Customer_NotExist
                    };
                    return(result);
                }
                Customer customer = new Customer(customerFromDb);
                string   code     = string.Empty;
                if (string.IsNullOrEmpty(customerFromDb.Code))
                {
                    long systemIdentity = await _commonService.GetNextId(typeof(Customer));

                    code = Common.Common.GenerateCodeFromId(systemIdentity, 3);
                }
                customer.Change(mesage);
                await _customerService.ChangeToDb(customer, code);

                result = new CommandResult()
                {
                    Message  = "",
                    ObjectId = customer.Id,
                    Status   = CommandResult.StatusEnum.Sucess
                };
                return(result);
            }
            catch (MessageException e)
            {
                e.Data["Param"] = mesage;
                ICommandResult result = new CommandResult()
                {
                    Message      = e.Message,
                    Status       = CommandResult.StatusEnum.Fail,
                    ResourceName = e.ResourceName
                };
                return(result);
            }
            catch (Exception e)
            {
                e.Data["Param"] = mesage;
                ICommandResult result = new CommandResult()
                {
                    Message = e.Message,
                    Status  = CommandResult.StatusEnum.Fail
                };
                return(result);
            }
        }
        public async Task <RCustomer> GetCurrentCustomer()
        {
            if (await IsAuthenticated())
            {
                if (_currentCustomer != null)
                {
                    return(_currentCustomer);
                }
                string key = _httpContextAccessor.HttpContext.User.FindFirst(SystemDefine.GicoAuId).Value;
                _currentCustomer = await _customerService.GetLoginInfoFromCache(key);

                return(_currentCustomer);
            }
            else
            {
                HttpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;
                throw new Exception("Unauthorized");
            }
        }
Esempio n. 4
0
        private async Task Login(string username, bool remember, RCustomer user)
        {
            try
            {
                string uniqueName = Common.Common.GenerateGuid();
                var    claims     = new[]
                {
                    new Claim(JwtRegisteredClaimNames.Email, username),
                    new Claim(JwtRegisteredClaimNames.UniqueName, uniqueName),
                    new Claim(SystemDefine.GicoAuId, uniqueName)
                };
                //var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(ConfigSettingEnum.JwtTokensKey.GetConfig()));
                //var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512);
                //JwtSecurityToken token = new JwtSecurityToken(ConfigSettingEnum.JwtTokensIssuer.GetConfig(),
                //    ConfigSettingEnum.JwtTokensAudience.GetConfig(),
                //    claims,
                //    expires: Extensions.GetCurrentDateUtc().AddMinutes(model.Remember ?
                //                                                                            ConfigSettingEnum.LoginExpiresTime.GetConfig().AsInt() + 60 :
                //                                                                            ConfigSettingEnum.LoginExpiresTime.GetConfig().AsInt()),
                //    signingCredentials: creds);
                //string tokenKey = new JwtSecurityTokenHandler().WriteToken(token);
                //response.TokenKey = new JwtSecurityTokenHandler().WriteToken(token);
                //response.IsAdministrator = true;
                //RRoleActionMapping[] roleActionMappings = await _roleService.RoleActionMappingGetByCustomerIdFromDb(user.Id);
                //response.ActionIds = roleActionMappings.Select(p => p.ActionId).ToArray();
                await _customerService.SetLoginToCache(uniqueName, user);

                var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                identity.AddClaims(claims);
                await _currentContext.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), new AuthenticationProperties()
                {
                    IsPersistent = true,
                    ExpiresUtc   = Extensions.GetCurrentDateUtc().AddMinutes(remember ? ConfigSettingEnum.LoginExpiresTime.GetConfig().AsInt() + 60
                        : ConfigSettingEnum.LoginExpiresTime.GetConfig().AsInt())
                });
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
            }
        }
Esempio n. 5
0
        public Customer(RCustomer customer)
        {
            ShardId        = customer.ShardId;
            Version        = customer.Version;
            Id             = customer.Id;
            Code           = customer.Code;
            CreatedDateUtc = customer.CreatedDateUtc;
            CreatedUid     = customer.CreatedUid;
            UpdatedDateUtc = customer.UpdatedDateUtc;
            UpdatedUid     = customer.UpdatedUid;
            LanguageId     = customer.LanguageId;
            StoreId        = customer.StoreId;
            Status         = customer.Status;

            Email                = customer.Email;
            EmailConfirmed       = customer.EmailConfirmed;
            IsTaxExempt          = customer.IsTaxExempt;
            FailedLoginAttempts  = customer.FailedLoginAttempts;
            LastIpAddress        = customer.LastIpAddress;
            Password             = customer.Password;
            PasswordSalt         = customer.PasswordSalt;
            PhoneNumber          = customer.PhoneNumber;
            PhoneNumberConfirmed = customer.PhoneNumberConfirmed;
            TwoFactorEnabled     = customer.TwoFactorEnabled;
            FullName             = customer.FullName;
            Gender               = customer.Gender;
            Birthday             = customer.Birthday;
            Type              = customer.Type;
            Status            = customer.Status;
            Version           = customer.Version;
            EmailToRevalidate = customer.EmailToRevalidate;
            AdminComment      = customer.AdminComment;
            BillingAddressId  = customer.BillingAddressId;
            ShippingAddressId = customer.ShippingAddressId;
            ExternalLogins    = customer.CustomerExternalLogins?.Select(p => new CustomerExternalLogin(p)).ToList();
        }
 public async Task SetLoginInfo(string key, RCustomer customer)
 {
     await RedisStorage.StringSet(key, customer,
                                  TimeSpan.FromMinutes(ConfigSettingEnum.LoginExpiresTime.GetConfig().AsInt()));
 }
Esempio n. 7
0
        public async Task <bool> ComparePassword(RCustomer customer, string loginPassword)
        {
            string passwordHash = EncryptionExtensions.Encryption(customer.Code, loginPassword, customer.PasswordSalt);

            return(await Task.FromResult(customer.Password.Equals(passwordHash)));
        }
Esempio n. 8
0
 public async Task SetLoginToCache(string key, RCustomer customer)
 {
     await _customerCacheStorage.SetLoginInfo(key, customer);
 }
Esempio n. 9
0
        public async Task <ICommandResult> Handle(CustomerExternalLoginAddCommand mesage)
        {
            try
            {
                ICommandResult result;
                RCustomer      customerFromDb = await _customerService.GetFromDb(mesage.CustomerId);

                if (customerFromDb == null)
                {
                    result = new CommandResult()
                    {
                        Message      = "",
                        ObjectId     = "",
                        Status       = CommandResult.StatusEnum.Fail,
                        ResourceName = ResourceKey.Customer_NotExist
                    };
                    return(result);
                }
                customerFromDb.CustomerExternalLogins = await _customerService.GetCustomerExternalLoginByCustomerId(customerFromDb.Id);

                Customer customer = new Customer(customerFromDb);
                CustomerExternalLogin customerExternalLogin = customer.AddExternalLogin(mesage);
                if (customerExternalLogin != null)
                {
                    await _customerService.Change(customer, customerExternalLogin);
                }
                if (!string.IsNullOrEmpty(mesage.VerifyId))
                {
                    RVerify rverify = await _emailSmsService.GetVerifyFromDb(mesage.VerifyId);

                    if (rverify != null)
                    {
                        Verify verify = new Verify(rverify);
                        verify.ChangeStatusToVerified();
                        await _emailSmsService.ChangeVerifyStatus(verify.Id, verify.Status);
                    }
                }
                result = new CommandResult()
                {
                    Message  = "",
                    ObjectId = customer.Id,
                    Status   = CommandResult.StatusEnum.Sucess
                };
                return(result);
            }
            catch (MessageException e)
            {
                e.Data["Param"] = mesage;
                ICommandResult result = new CommandResult()
                {
                    Message      = e.Message,
                    Status       = CommandResult.StatusEnum.Fail,
                    ResourceName = e.ResourceName
                };
                return(result);
            }
            catch (Exception e)
            {
                e.Data["Param"] = mesage;
                ICommandResult result = new CommandResult()
                {
                    Message = e.Message,
                    Status  = CommandResult.StatusEnum.Fail
                };
                return(result);
            }
        }