آیا زمان ثبت نام در سمت سرور نباید هم access_token و هم refresh_token رو تولید و به سمت کلاینت ارسال کرد؟
//do register action and send sms message to client
//then call activateRegisterCode action from client to server
//In the following run operation authentication for user
[AllowAnonymous]
[HttpPost("[action]")]
[IgnoreAntiforgeryToken]
public async Task<IActionResult> ActivateRegisterCode([FromBody] ActiveCodeModel model)
{
if (model == null)
{ return BadRequest("درخواست نامعتبر"); }
var user = await _userService.FindUserByKeyCodeAsync(model.Keycode);
if (user == null)
{
return BadRequest("کد امنیتی معتبر نیست لطفا مجددا درخواست کد امنیتی کنید");
}
user.IsActive = true;
var userUpdated = await _userService.UpdateAsync(user, CancellationToken.None);
var jwt = await _tokenFactoryService.CreateJwtTokensAsync(userUpdated);
this.Response.Headers.Add("x-auth-token", jwt.AccessToken);
this.Response.Headers.Add("access-control-expose-headers", "x-auth-token");
_antiforgery.RegeneratedAntiForgeryCookie(jwt.Claims);
return Ok();
}
در ادامه پیاده سازی action logout برای حذف توکنها از دیتابیس
[AllowAnonymous]
[HttpGet("[action]"), HttpPost("[action]")]
public async Task<bool> Logout(string refreshtoken)
{
var claimsIdentity = this.User.Identity as ClaimsIdentity;
var userIdValue = claimsIdentity.FindFirst(ClaimTypes.UserData)?.Value;
if (!string.IsNullOrWhiteSpace(userIdValue) && Guid.TryParse(userIdValue, out Guid userId))
{
await _tokenStoreService.InvalidateUserTokensAsync(userId).ConfigureAwait(false);
}
await _tokenStoreService.DeleteExpiredTokensAsync().ConfigureAwait(false);
await _uow.SaveChangesAsync().ConfigureAwait(false);
_antiforgery.DeleteAntiForgeryCookie();
return true;
}
مشکلی که هست تو اکشن logout شی userIdValue خالیه و بنظر میرسه که کوکی مقدار نداره!
آیا باید زمان فراخوانی این اکشن refreshtoken رو هم بهش ارسال کنیم؟
تو اکشن لاگین هم کوکیها مقدار نمیگیره یا اینکه اصلا با کوکیها کار نداریم و بر اساس refreshtoken باید مقادیر claimهای داخلش رو decode و بررسی کرد و سپس بر اساس اون در بانک اطلاعاتی عملیات خذف توکنهای صادر شده در زمان ثبت نام و یا لاگین رو انجام داد.