添加项目文件。
This commit is contained in:
34
TechHelper.Client/HttpRepository/RefreshTokenService.cs
Normal file
34
TechHelper.Client/HttpRepository/RefreshTokenService.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
|
||||
namespace TechHelper.Client.HttpRepository
|
||||
{
|
||||
public class RefreshTokenService
|
||||
{
|
||||
private readonly AuthenticationStateProvider _authenticationStateProvider;
|
||||
private readonly IAuthenticationClientService _authenticationClientService;
|
||||
|
||||
public RefreshTokenService(AuthenticationStateProvider authenticationStateProvider, IAuthenticationClientService authenticationClientService)
|
||||
{
|
||||
_authenticationStateProvider = authenticationStateProvider;
|
||||
_authenticationClientService = authenticationClientService;
|
||||
}
|
||||
|
||||
public async Task<string> TryRefreshToken()
|
||||
{
|
||||
var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
|
||||
var user = authState.User;
|
||||
var expClaim = user.FindFirst(c => c.Type.Equals("exp")).Value;
|
||||
var expTime = DateTimeOffset.FromUnixTimeSeconds(
|
||||
Convert.ToInt64(expClaim));
|
||||
|
||||
var diff = expTime - DateTime.UtcNow;
|
||||
if (diff.TotalMinutes <= 2)
|
||||
return await _authenticationClientService.RefreshTokenAsync();
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user