chore
This commit is contained in:
38
Atomx.Admin/Atomx.Admin.Client/Utils/ClientTokenProvider.cs
Normal file
38
Atomx.Admin/Atomx.Admin.Client/Utils/ClientTokenProvider.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Atomx.Admin.Client.Services;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace Atomx.Admin.Client.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// WASM 客户端下的 Token 提供器(实现共享的 ITokenProvider)
|
||||
/// - 直接从浏览器 storage(localStorage/sessionStorage)读取 access token
|
||||
/// - 设计为轻量,仅负责读取 token;刷新逻辑放在 AuthHeaderHandler / 后端 Refresh 接口
|
||||
/// </summary>
|
||||
public class ClientTokenProvider : ITokenProvider
|
||||
{
|
||||
private readonly IJSRuntime _jsRuntime;
|
||||
|
||||
public ClientTokenProvider(IJSRuntime jsRuntime)
|
||||
{
|
||||
_jsRuntime = jsRuntime;
|
||||
}
|
||||
|
||||
public async Task<string?> GetTokenAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _jsRuntime.InvokeAsync<string>("localStorage.getItem", "accessToken");
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> IsTokenValidAsync()
|
||||
{
|
||||
var token = await GetTokenAsync();
|
||||
return !string.IsNullOrEmpty(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user