Asp.net core 服务器端blazor中的会话超时重定向
所以Blazor目前似乎不支持使用滑动过期方案通过不活动来检查身份验证超时Asp.net core 服务器端blazor中的会话超时重定向,asp.net-core,blazor,blazor-server-side,Asp.net Core,Blazor,Blazor Server Side,所以Blazor目前似乎不支持使用滑动过期方案通过不活动来检查身份验证超时 我试图实现的是,一旦用户有了会话时间,就将其重定向到登录页面。我可以想象,它必须是在重新验证IdentityAuthenticationStateProvider中的某个地方,我需要检测站点上的活动,并在会话超时时重定向登录页面上的用户,但不确定如何实现这一点?您实际上不需要使用 重新验证IdentityAuthenticationStateProvider 你的app.razor只需要如下代码: <Cascadi
我试图实现的是,一旦用户有了会话时间,就将其重定向到登录页面。我可以想象,它必须是在
重新验证IdentityAuthenticationStateProvider
中的某个地方,我需要检测站点上的活动,并在会话超时时重定向登录页面上的用户,但不确定如何实现这一点?您实际上不需要使用
重新验证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组件在AfterRender回调上重定向用户
应该是这样的
[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)
{
}
}
[CascadingParameter]
私有任务AuthenticationStateTask{get;set;}
AfterRenderAsync(bool firstRender)上受保护的重写异步任务
{
var authenticationState=等待AuthenticationStateTask;
尝试
{
if(authenticationState?.User?.Identity为null | |!authenticationState.User.Identity.IsAuthenticated)
{
var returnUrl=Navigation.ToBaseRelativePath(Navigation.Uri);
if(string.IsNullOrWhiteSpace(returnUrl))
Navigation.NavigateTo(“/Identity/Account/Login”,true);
其他的
Navigation.NavigateTo($”/Identity/Account/Login?returnUrl=~/{returnUrl}),true);
}
}
捕获(例外e)
{
}
}