Esempio n. 1
0
        public async Task <UserTokenResponseDto> Handle(GetUserTokenQuery request, CancellationToken cancellationToken)
        {
            var token = await _tokenGenerator.GenerateTokenAsync(request.UserName, request.Password);

            var result = _mapper.Map <UserTokenResponseDto>(token);

            return(result);
        }
        public async Task GenerateToken_UserNotFound()
        {
            _repositoryUser
            .Setup(x => x.FindFirstAsync(It.IsAny <Specification <DatabaseUser> >()))
            .ReturnsAsync((DatabaseUser)null);

            var request = new TokenRequest("email", "password");

            await Assert.ThrowsAsync <EducationSystemNotFoundException>
                (() => _tokenGenerator.GenerateTokenAsync(request));
        }
        private async Task <Token> GenerateTokenAndUpdateUserAsync(User user)
        {
            Token token = new Token {
                AccessToken = await tokenGenerator.GenerateTokenAsync(user)
            };

            token.RefreshToken           = tokenGenerator.RefreshTokenGeneretor();
            user.RefreshToken            = token.RefreshToken;
            user.ExpiredRefreshTokenDate = DateTime.UtcNow.AddDays(7);
            await userRepo.UpdateAsync(user);

            return(token);
        }
Esempio n. 4
0
        private async Task <Token> GetTokensAsync(RefreshCommand context) // tokens : access and refresh token
        {
            var user = await GetUserFromExpiredTokenAsync(context.AccessToken);

            if (user == null || user.RefreshToken != context.RefreshToken || user.ExpiredRefreshTokenDate <= DateTime.UtcNow)
            {
                throw new SecurityException();
            }
            var newAccessToken = await tokenGenerator.GenerateTokenAsync(user);

            var newRefreshToken = tokenGenerator.RefreshTokenGeneretor();

            //update user
            user.RefreshToken = newRefreshToken;
            await userRepo.UpdateAsync(user);

            return(new Token {
                RefreshToken = newRefreshToken, AccessToken = newAccessToken
            });
        }
Esempio n. 5
0
        public async Task <IActionResult> Login([FromBody] UserDto userDto)
        {
            var userInDb = await _userManager.FindByNameAsync(userDto.UserName);

            if (userInDb == null)
            {
                return(NotFound());
            }

            var result = await _signInManager.CheckPasswordSignInAsync(userInDb, userDto.Password, false);

            if (!result.Succeeded)
            {
                return(Unauthorized());
            }

            var token = await _tokenGenerator.GenerateTokenAsync(userInDb, _config, _userManager);

            return(Ok(new
            {
                token
            }));
        }
Esempio n. 6
0
        public async Task <ActionResult> LoginAsync([FromBody] LoginDto loginInfo)
        {
            string userToken = await tokenGenerator.GenerateTokenAsync(loginInfo.Email, loginInfo.Password);


            if (userToken != null)
            {
                var response = new
                {
                    accessToken = $"Bearer {userToken}"
                };

                Response.ContentType = "application/json";
                await Response.WriteAsync(JsonConvert.SerializeObject(response, new JsonSerializerSettings {
                    Formatting = Formatting.Indented
                }));

                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
Esempio n. 7
0
 public async Task <IActionResult> Generate([FromBody] TokenRequest request)
 {
     return(await Ok(() => _tokenGenerator.GenerateTokenAsync(request)));
 }