[英]Session Timeout redirect in Server side blazor
So it seems like Blazor does not currently support checking for an authentication timeout through inactivity using sliding expiration scheme.因此,Blazor 目前似乎不支持使用滑动过期方案通过不活动来检查身份验证超时。
what I am trying to achieve is to redirect user to login page once they had a session time.我想要实现的是在用户有会话时间后将用户重定向到登录页面。 I can imagine that it has to be something in
RevalidatingIdentityAuthenticationStateProvider
where I need to detect activity on the site and redirect the user on the login page if the session has timeout but not sure how can I achieve this?我可以想象它必须是
RevalidatingIdentityAuthenticationStateProvider
中的某些内容,如果会话超时,我需要检测站点上的活动并在登录页面上重定向用户,但不确定如何实现这一点?
you don't actually need to use你实际上并不需要使用
RevalidatingIdentityAuthenticationStateProvider
RevalidatingIdentityAuthenticationStateProvider
your app.razor just needs to look like this code:你的 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>
make sure that RedirectToLogin component redirect the user OnAfterRender callback should looks something like this确保 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)
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.