因此,看来Blazor当前不支持使用滑动过期方案通过不活动来检查身份验证超时。
我要实现的目的是在用户拥有会话时间后将其重定向到登录页面。我可以想象它必须是RevalidatingIdentityAuthenticationStateProvider
中的某个内容,如果会话超时,我需要检测网站上的活动并在登录页面上重定向用户,但不确定如何实现?
答案 0 :(得分:0)
您实际上不需要使用
RevalidatingIdentityAuthenticationStateProvider
您的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)
{
}
}