繁体   English   中英

服务器端 blazor 中的会话超时重定向

[英]Session Timeout redirect in Server side blazor

因此,Blazor 目前似乎不支持使用滑动过期方案通过不活动来检查身份验证超时。

我想要实现的是在用户有会话时间后将用户重定向到登录页面。 我可以想象它必须是RevalidatingIdentityAuthenticationStateProvider中的某些内容,如果会话超时,我需要检测站点上的活动并在登录页面上重定向用户,但不确定如何实现这一点?

你实际上并不需要使用

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)
        {

            
        }
       
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM