我的令牌过期时遇到问题。令牌在服务器端过期,但在客户端,仅当我按 F5 时才显示过期。
App.razor代码
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<MCF.BlazorClient.Components.RedirectToLogin></MCF.BlazorClient.Components.RedirectToLogin>
</NotAuthorized>
<Authorizing>
<h1>Authentication in progress</h1>
</Authorizing>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
Run Code Online (Sandbox Code Playgroud)
我的 AuthService 代码:
public async Task<LoginResult> Login(LoginModel loginModel)
{
var result = await _httpClient.PostJsonAsync<LoginResult>(GetApi("api/login"), loginModel);
if (result.Successful)
{
await SetTokenAsync(result.Token, result.Expiry);
((ApiAuthenticationStateProvider)_authenticationStateProvider).MarkUserAsAuthenticated(result.Token);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", result.Token);
return result;
}
return result;
}
public async Task SetTokenAsync(string token, DateTime expiry = default)
{
if (token …Run Code Online (Sandbox Code Playgroud)