Esempio n. 1
0
        public async Task <IActionResult> Authenticate([FromBody] LoginDto login)
        {
            var loginResultDto = new LoginResultDto();

            var result = await _signInManager.PasswordSignInAsync(login.Email, login.Password, true, lockoutOnFailure : false); // Check the user's email and password

            if (result.Succeeded)
            {
                var token = _authenticationService.GenerateAuthToken(login.Email, _appSettings.Secret); // The login was successful so generate a auth token and send it to the user along with a refresh token

                if (string.IsNullOrEmpty(token))
                {
                    return(BadRequest(new { message = "Username or password is incorrect" }));
                }

                var user = await _userManager.FindByEmailAsync(login.Email);

                _authenticationService.RemoveRefreshTokenByUser(user.Id, "providername", "refresh", "phone");         // Remove any existing refresh tokens for the user so that we can add the new one
                var newRefreshToken = _authenticationService.GenerateRefreshToken();                                  // Generate a new refresh token
                _authenticationService.AddRefreshToken(user.Id, "providername", "refresh", newRefreshToken, "phone"); // Save the new refresh token

                loginResultDto.WasSuccessful = true;
                loginResultDto.Token         = token;
                loginResultDto.RefreshToken  = newRefreshToken;

                return(Ok(loginResultDto));
            }

            loginResultDto.WasSuccessful = false;

            return(Ok(loginResultDto));
        }