Проверьте эту ссылку Отличное объяснение для обновления токена JWT.
Проверьте код на стороне сервера
Создать токен
[HttpPost("Authenticate")]
[AllowAnonymous]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(TokenResponse), StatusCodes.Status200OK)]
public async Task<IActionResult> Authenticate([FromBody]LoginModel model)
{
// get current user
var user = await _accountService.FindByEmailAsync(model.Email);
if (user != null)
{
var result = await _accountService.CheckPasswordSignInAsync(user, model.Password, false);
if (result.Succeeded)
{
// check refresh token delete if exits and recreate new
var refreshToken = await _refreshTokenService.GetRefreshToken(user);
if (refreshToken != null)
await _refreshTokenService.DeleteRefreshToken(refreshToken);
var newRefreshToken = new RefreshToken
{
UserId = user.Id,
Token = Guid.NewGuid().ToString(),
IssuedUtc = DateTime.Now.ToUniversalTime(),
ExpiresUtc = DateTime.Now.ToUniversalTime().AddMinutes(Convert.ToDouble(_appSettings.ExpireMinutesTokenRefresh))
};
await _refreshTokenService.CreateRefreshToken(newRefreshToken);
// generate jwt token
var token = GenerateJwtToken(model.Email, user, out DateTime expires);
var response = new TokenResponse
{
AccessToken = token,
RefreshToken = newRefreshToken.Token,
FirstName = newRefreshToken.User.FirstName,
LastName = newRefreshToken.User.LastName,
TokenExpiration = expires,
};
return Ok(response);
}
else
{
return BadRequest("Login or password incorrect");
}
}
else
{
return BadRequest("Login or password incorrect");
}
}
Жетон обновления
[HttpPost]
[AllowAnonymous]
[Route("Token/Refresh")]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(TokenResponse), StatusCodes.Status200OK)]
public async Task<IActionResult> RefreshToken([FromBody] RefreshToken refreshToken)
{
var refreshTokenFromDatabase = await _refreshTokenService.GetRefreshToken(refreshToken);
if (refreshTokenFromDatabase == null)
return BadRequest();
if (refreshTokenFromDatabase.ExpiresUtc < DateTime.Now.ToUniversalTime())
return Unauthorized();
if (!await _accountService.CanSignInAsync(refreshTokenFromDatabase.User))
return Unauthorized();
//if (_accountService.SupportsUserLockout && await _accountService.IsLockedOutAsync(refreshTokenFromDatabase.User))
// return Unauthorized();
var token = GenerateJwtToken(refreshTokenFromDatabase.User.Email, refreshTokenFromDatabase.User, out DateTime expires, true);
var response = new TokenResponse
{
AccessToken = token,
RefreshToken = refreshTokenFromDatabase.Token,
FirstName = refreshTokenFromDatabase.User.FirstName,
LastName = refreshTokenFromDatabase.User.LastName,
TokenExpiration = expires
};
return Ok(response);
}