Пример #1
0
        public async Task <IActionResult> Login([FromBody] UserDTO userDto)
        {
            if (string.IsNullOrEmpty(userDto.Username) || string.IsNullOrEmpty(userDto.Password))
            {
                return(BadRequest("Username/password cannot be empty"));
            }

            var user = await userManager.FindByNameAsync(userDto.Username);

            if (user != null && await userManager.CheckPasswordAsync(user, userDto.Password))
            {
                var refreshToken = TokenFactory.GenerateToken();

                refreshTokenRepository.AddRefreshToken(
                    new RefreshToken
                {
                    Token             = refreshToken,
                    ApplicationUserID = user.Id,
                    ExpiryDate        = DateTime.Now.AddDays(5)
                });

                var jwt = await GenerateJwt(user);

                return(Ok(new { jwt, refreshToken }));
            }

            return(BadRequest("Error while logging in. Please try later."));
        }
Пример #2
0
        public IActionResult Login(UserCredentials credentials)
        {
            var verifiedCredentials = _userRepository.VerifyCredentials(credentials);

            if (verifiedCredentials == null)
            {
                return(BadRequest("Username or password is incorrect!"));
            }

            var accessToken  = _jwtHelper.GetAccessToken(verifiedCredentials);
            var refreshToken = _jwtHelper.GetRefreshToken();

            if (!_refreshTokenRepository.AddRefreshToken(refreshToken, verifiedCredentials.Id))
            {
                return(Unauthorized());
            }

            return(Ok(new Token(accessToken, refreshToken)));
        }