【问题标题】:Blazor Custom Authentication State ProviderBlazor 自定义身份验证状态提供程序
【发布时间】:2021-09-11 09:21:23
【问题描述】:

我创建了一个自定义身份验证状态提供程序,用于检查我们内部 LDAP 服务上的用户名和密码。 我的以下代码在初始登录期间工作正常。但是登录后,如果我按 F5 或刷新页面,它会自动进入登录页面。 有人可以帮忙吗?

public class CustomAuthenticationStateProvider : AuthenticationStateProvider
{
    private readonly ILdapAuthenticationService _ldapAuthenticationService;
    private ClaimsPrincipal _cachedClaimsPrincipal;

    public CustomAuthenticationStateProvider(ILdapAuthenticationService 
ldapAuthenticationService)
    {
        _ldapAuthenticationService = ldapAuthenticationService;
    }

    public override async 
Task<Microsoft.AspNetCore.Components.Authorization.AuthenticationState>
        GetAuthenticationStateAsync()
    {
        if (_cachedClaimsPrincipal != null)
            return await Task.FromResult(
                new 
Microsoft.AspNetCore.Components.Authorization.AuthenticationState(_cachedClaimsPrincipal));

        return await Task.FromResult(new 
Microsoft.AspNetCore.Components.Authorization.AuthenticationState(new ClaimsPrincipal(new 
ClaimsIdentity())));
    }


    public void ValidateLogin(string username, string password)
    {
        if (string.IsNullOrEmpty(username)) throw new Exception("Enter username");
        if (string.IsNullOrEmpty(password)) throw new Exception("Enter password");

        if (_ldapAuthenticationService.AuthenticateUser(username, password))
        {
            _cachedClaimsPrincipal = _ldapAuthenticationService.CurrentUser.ClaimsPrincipal;
        }
        
        NotifyAuthenticationStateChanged(
            Task.FromResult(new 
 AuthenticationState(_ldapAuthenticationService.CurrentUser.ClaimsPrincipal)));
    }
}

public class RedirectToLogin : ComponentBase
{
    [Inject]
    protected NavigationManager NavigationManager { get; set; }

    [CascadingParameter]
    private Task<Microsoft.AspNetCore.Components.Authorization.AuthenticationState> 
authenticationStateTask { get; set; }

    protected override void OnInitialized()
    {
        NavigationManager.NavigateTo("/");
    }
}

创建了RedirectToLogin,以便用户在浏览任何页面之前必须进行身份验证

【问题讨论】:

    标签: blazor blazor-server-side asp.net-blazor


    【解决方案1】:

    您的 CustomAuthenticationStateProvider 的作用域将在请求后终止,因此将 ClaimsPrincipal 缓存在此类的本地成员中将不起作用:

    private ClaimsPrincipal _cachedClaimsPrincipal;
    

    在构造函数上添加调试日志,您将看到它为每个请求创建(因此 _cachedClaimsPrincipal 中的任何存储值都丢失了)。

    public CustomAuthenticationStateProvider(ILdapAuthenticationService 
        ldapAuthenticationService)
    {
        System.Diagnostics.Debug.WriteLine("Constructor");
        _ldapAuthenticationService = ldapAuthenticationService;
    }
    

    您需要将其缓存在持久位置(例如,服务器端的会话对象或客户端的本地存储)。

    【讨论】:

    • 我是 Blazor 的新手。您能帮我了解如何存储在 Session 对象上吗?
    • 刚刚重新阅读了您的问题,我也用“本地存储”更新了我的答案。会话仅在服务器端可用。有关本地(浏览器)存储的指导,请参阅此链接...docs.microsoft.com/en-us/aspnet/core/blazor/…
    猜你喜欢
    • 2020-07-23
    • 2012-04-05
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 2017-11-21
    相关资源
    最近更新 更多