0

我像这样实现我的自定义 ServerAuthenticationStateProvider 。

public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
    var identity = new ClaimsIdentity();
    try
    {
        var userInfo = await GetCurrentUser();
        if (userInfo.IsAuthenticated)
        {
            var claims = new[] { new Claim(ClaimTypes.Name, _currentUser.UserName) }.Concat(_currentUser.Claims.Select(c => new Claim(c.Key, c.Value))); ;
            identity = new ClaimsIdentity(claims, "Server authentication");
        }
    }
    catch (HttpRequestException ex)
    {
        Console.WriteLine("Request failed:" + ex.ToString());
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error:" + ex.ToString());
    }
    return new AuthenticationState(new ClaimsPrincipal(identity));
} 
public async Task Login(LoginViewModel loginParameters)
{
    try
    {
        await api.Login(loginParameters);
        SetAuthenticationState(GetAuthenticationStateAsync());
        NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error:" + ex.ToString());
    }
}

运行我的项目并登录时,AuthenticationState 和 ServerAuthenticationStateProvider 结果不同 在此处输入图像描述

如何更改 AuthenticationState?

这种差异使得 AuthorizeView 无法正常工作

4

0 回答 0