添加项目文件。
This commit is contained in:
40
Atomx.Admin/Atomx.Admin.Client/Utils/TokenProvider.cs
Normal file
40
Atomx.Admin/Atomx.Admin.Client/Utils/TokenProvider.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using Atomx.Common.Configuration;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace Atomx.Admin.Client.Utils
|
||||
{
|
||||
public interface ITokenProvider
|
||||
{
|
||||
Task<string?> GetTokenAsync();
|
||||
Task<bool> IsTokenValidAsync();
|
||||
}
|
||||
|
||||
public class ClientTokenProvider : ITokenProvider
|
||||
{
|
||||
private readonly IJSRuntime _jsRuntime;
|
||||
|
||||
public ClientTokenProvider(IJSRuntime jsRuntime)
|
||||
{
|
||||
_jsRuntime = jsRuntime;
|
||||
}
|
||||
|
||||
public async Task<string?> GetTokenAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 从localStorage或sessionStorage获取token
|
||||
return await _jsRuntime.InvokeAsync<string>("localStorage.getItem", StorageKeys.JWTTokenKeyName);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> IsTokenValidAsync()
|
||||
{
|
||||
var token = await GetTokenAsync();
|
||||
return !string.IsNullOrEmpty(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user