因此,Blazor目前似乎不支持使用滑动过期方案通过不活动来检查身份验证超时。
我试图实现的是,一旦用户有了会话时间,就将其重定向到登录页面。我可以想象,在RevalidatingIdentityAuthenticationStateProvider
中,如果会话超时,我需要检测网站上的活动,并在登录页面上重定向用户,但不确定如何实现这一点?
您实际上不需要使用
重新验证IdentityAuthenticationStateProvider
你的app.razor只需要看起来像这样的代码:
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData"
DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
**<RedirectToLogin> </RedirectToLogin>**
</NotAuthorized>
<Authorizing>
<h1>Authentication in progress</h1>
<p>Only visible while authentication is in progress.</p>
</Authorizing>
</AuthorizeRouteView>
</Found>
<NotFound>
....
</NotFound>
</Router>
</CascadingAuthenticationState>
确保RedirectToLogin组件重定向用户OnAfterRender回调应该看起来像这个
[CascadingParameter]
private Task<AuthenticationState> AuthenticationStateTask { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
var authenticationState = await AuthenticationStateTask;
try
{
if (authenticationState?.User?.Identity is null || !authenticationState.User.Identity.IsAuthenticated)
{
var returnUrl = Navigation.ToBaseRelativePath(Navigation.Uri);
if (string.IsNullOrWhiteSpace(returnUrl))
Navigation.NavigateTo("/Identity/Account/Login", true);
else
Navigation.NavigateTo($"/Identity/Account/Login?returnUrl=~/{returnUrl}", true);
}
}
catch (Exception e)
{
}
}