예제 #1
0
        public async Task <AuthorizationResultDto> CreateUserAsync(RegisterUserDto userDto)
        {
            var user   = _mapper.Map <RegisterUserDto, User>(userDto);
            var result = await _userManager.CreateAsync(user, userDto.Password);

            var registrationResult = new AuthorizationResultDto();

            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(user, Role.User.ToString());

                await _signInManager.SignInAsync(user, false);

                registrationResult.IsSuccessful = true;
                registrationResult.Token        = await _tokenService.GetTokenAsync(user);

                registrationResult.UserName = userDto.UserName;
                registrationResult.Email    = userDto.Email;
                registrationResult.Role     = Role.User.ToString();

                return(registrationResult);
            }

            foreach (var error in result.Errors)
            {
                registrationResult.Errors.Add(error.Description);
            }

            return(registrationResult);
        }
예제 #2
0
        public async Task EditRole_Method_Test_EditRole_Call_Once_Return_Not_Null_Result()
        {
            // Arrange
            var model = new EditRoleViewModel()
            {
                Request = new EditRoleUserDto()
            };
            var returnModel = new AuthorizationResultDto();

            _userService.Setup(f => f.EditRole(model.Request)).ReturnsAsync(returnModel);

            var controller = new AccountController(_userService.Object);

            // Act
            var result = await controller.EditRole(model);

            // Assert
            Assert.IsNotNull(result);
            _userService.Verify(f => f.EditRole(model.Request), Times.Once);
        }
예제 #3
0
        public async Task <AuthorizationResultDto> EditRole(EditRoleUserDto roleDto)
        {
            var user = await _userManager.FindByNameAsync(roleDto.UserName);

            var editResult = new AuthorizationResultDto();

            if (user != null)
            {
                var userRoles = await _userManager.GetRolesAsync(user);

                await _userManager.RemoveFromRolesAsync(user, userRoles);

                await _userManager.AddToRoleAsync(user, roleDto.Role);

                editResult.IsSuccessful = true;
                editResult.UserName     = user.UserName;
                editResult.Role         = (await _userManager.GetRolesAsync(user)).First();
            }

            return(editResult);
        }
예제 #4
0
        public async Task <AuthorizationResultDto> LoginAsync(LoginUserDto userDto)
        {
            var result = await _signInManager.PasswordSignInAsync(userDto.UserName, userDto.Password, false, false);

            var loginResult = new AuthorizationResultDto();

            if (result.Succeeded)
            {
                var user = await _userManager.FindByNameAsync(userDto.UserName);

                loginResult.Token = await _tokenService.GetTokenAsync(user);

                loginResult.IsSuccessful = true;
                loginResult.UserName     = userDto.UserName;
                loginResult.Role         = (await _userManager.GetRolesAsync(user)).First();

                return(loginResult);
            }

            loginResult.Errors.Add("Invalid username or password");

            return(loginResult);
        }
예제 #5
0
        public async Task Loginr_Method_Test_LoginAsync_Call_Once_Return_Not_Null_Result()
        {
            // Arrange
            var request = new LoginUserDto();
            var model   = new LoginUserViewModel()
            {
                Request = request
            };
            var returnModel = new AuthorizationResultDto()
            {
                IsSuccessful = true
            };

            _userService.Setup(f => f.LoginAsync(model.Request)).ReturnsAsync(returnModel);

            var controller = new AccountController(_userService.Object);

            // Act
            var result = await controller.Login(model);

            // Assert
            Assert.IsNotNull(result);
            _userService.Verify(f => f.LoginAsync(request), Times.Once);
        }