示例#1
0
        public async Task <JsonRedirect> UpdateUserModel(AuthViewModel.SignUpModel model)
        {
            var _validations = new AuthSignUpModelValidator();

            var results = _validations.Validate(model);

            if (!results.IsValid)
            {
                var failure = results.Errors.FirstOrDefault();

                return(new JsonRedirect(failure.ErrorMessage));
            }

            var userViewModel = _mapper.Map <AuthViewModel.SignUpModel, UserViewModel>(model);

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            if (user.Name != userViewModel.Name ||
                user.SecondName != userViewModel.SecondName ||
                user.Email != userViewModel.Email ||
                user.PhoneNumber != userViewModel.PhoneNumber)
            {
                var passwordHashNew = _userManager.PasswordHasher.HashPassword(user, userViewModel.Password);

                user.Name = userViewModel.Name;
                user.NormalizedUserName = userViewModel.Name.ToUpper();
                user.UserName           = userViewModel.Name;
                user.SecondName         = userViewModel.SecondName;
                user.PhoneNumber        = userViewModel.PhoneNumber;
                user.Email           = userViewModel.Email;
                user.NormalizedEmail = userViewModel.Email.ToUpper();
                user.PasswordHash    = passwordHashNew;
                user.UpdatedDate     = DateTime.Now;

                var updateResult = await _userManager.UpdateAsync(user);

                if (!updateResult.Succeeded)
                {
                    var failure = updateResult.Errors.FirstOrDefault().Description;

                    return(new JsonRedirect(failure));
                }

                await _signInManager.RefreshSignInAsync(user);

                return(new JsonRedirect("The user has been updated successfully."));
            }
            else
            {
                return(new JsonRedirect("No changes in the fields."));
            }
        }
示例#2
0
        public async Task <JsonRedirect> SignUp(AuthViewModel model)
        {
            var _validations = new AuthSignUpModelValidator();

            var results = _validations.Validate(model.SignUp);

            if (!results.IsValid)
            {
                var failure = results.Errors.FirstOrDefault();

                return(new JsonRedirect(failure.ErrorMessage));
            }

            var user = await _userManager.FindByNameAsync(model.SignUp.Name);

            if (user != null)
            {
                return(new JsonRedirect("The user is already registered."));
            }

            var newUser = new User()
            {
                Id           = Guid.NewGuid(),
                UserName     = model.SignUp.Name,
                Name         = model.SignUp.Name,
                SecondName   = model.SignUp.SecondName,
                PhoneNumber  = model.SignUp.PhoneNumber,
                Email        = model.SignUp.Email,
                PasswordHash = model.SignUp.Password,
                CreatedDate  = DateTime.Now,
                UpdatedDate  = DateTime.Now
            };

            var result = await _userManager.CreateAsync(newUser, model.SignUp.Password);

            if (result.Succeeded)
            {
                await _userManager.AddClaimAsync(newUser, new Claim(ClaimTypes.Role, ClaimPolicyMatch.USER));

                await _signInManager.PasswordSignInAsync(newUser, model.SignUp.Password, false, false);

                await _basketService.Create(newUser.Id);

                return(new JsonRedirect(new Link(nameof(StoreController), nameof(StoreController.Index))));
            }

            return(new JsonRedirect(result.Errors.FirstOrDefault().Code));
        }