【问题标题】:Why my Authentication State Provider not work?为什么我的身份验证状态提供程序不起作用?
【发布时间】:2020-06-11 20:12:05
【问题描述】:

我将 blazor 与服务器端渲染一起使用,我想做自己的 AuthenticationStateProvider,但它不起作用,我不知道为什么。

我在 public class LocalAuthenticationStateProvider 中的 ovveride 方法:AuthenticationStateProvider

    public async override Task<AuthenticationState> GetAuthenticationStateAsync()
    {
        if (await _storageService.ContainKeyAsync("User"))
        {
            var userInfo = await _storageService.GetItemAsync<LocalUserInfo>("User");

            var claims = new[]
            {
                new Claim("Email", userInfo.Email),
                new Claim("FirstName", userInfo.FirstName),
                new Claim("LastName", userInfo.LastName),
                new Claim("AccessToken", userInfo.AccessToken),
                new Claim(ClaimTypes.NameIdentifier, userInfo.Id),
            };

            var identity = new ClaimsIdentity(claims, "BearerToken");
            var user = new ClaimsPrincipal(identity);
            var state = new AuthenticationState(user);
            NotifyAuthenticationStateChanged(Task.FromResult(state));
            return state;
        }

        return new AuthenticationState(new ClaimsPrincipal());
    }

我的登录页面:

@inject AuthenticationStateProvider authenticationStateProvider

await storageService.SetItemAsync("User", userInfo);
await authenticationStateProvider.GetAuthenticationStateAsync();

navigationManager.NavigateTo("/");

我的 Startup.cs

        services.AddAuthentication();
        services.AddAuthorization();
        services.AddAuthorizationCore();
        services.AddScoped<AuthenticationStateProvider, LocalAuthenticationStateProvider>();

我的 Index.razor 用于检查身份验证。这总是未授权

<AuthorizeView>
<Authorized>
    <h1>Hi! @context.User.FindFirst("FirstName").Value</h1>
</Authorized>
<NotAuthorized>
    <RadzenButton Text="login" Click="GoToRegister"></RadzenButton>
</NotAuthorized>
<Authorizing>
    <h1>Authentication in progress</h1>
    <p>Only visible while authentication is in progress.</p>
</Authorizing>

这有什么问题?

【问题讨论】:

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


    【解决方案1】:

    在您的登录页面中,您应该注入LocalAuthenticationStateProvider

    @inject LocalAuthenticationStateProvider LocalAuthStateProvider
    

    做完之后:

    await storageService.SetItemAsync("User", userInfo);
    

    我猜,存储用户的信息,让订阅者,比如CascadingAuthenticationState,知道Authentication状态已经改变了。 这是这样做的:

    LocalAuthStateProvider.NotifyAuthenticationStateChanged();
    

    您的自定义 AuthenticationStateProvider 应如下所示:

    public async override Task<AuthenticationState> GetAuthenticationStateAsync()
    {
        ClaimsIdentity identity;
    
        if (await _storageService.ContainKeyAsync("User"))
        {
            var userInfo = await _storageService.GetItemAsync<LocalUserInfo> 
                                                                    ("User");
    
            var claims = new[]
            {
                new Claim("Email", userInfo.Email),
                new Claim("FirstName", userInfo.FirstName),
                new Claim("LastName", userInfo.LastName),
                new Claim("AccessToken", userInfo.AccessToken),
                new Claim(ClaimTypes.NameIdentifier, userInfo.Id),
            };
    
            identity = new ClaimsIdentity(claims );
    
    
        }
        else
        {
              identity = new ClaimsIdentity();
        }
        return await Task.FromResult(new AuthenticationState(new 
                                                 ClaimsPrincipal(identity)));
    
    }
    
    public void NotifyAuthenticationStateChanged()
    {
            NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
     }
    

    你的 Startup 类应该是这样的:

    public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication();
            services.AddAuthorization();
           // services.AddAuthorizationCore();
    
            services.AddScoped<LocalAuthenticationStateProvider>();
            services.AddScoped<AuthenticationStateProvider>(provider => provider.GetRequiredService<LocalAuthenticationStateProvider>());
    
        }
    

    希望这会有所帮助...

    【讨论】:

    • 嗨!我只是尝试一下,但它不起作用。所以我看到了这个调用方法 GetAuthenticationStateAsync() 并且通过调试我看到了这个添加的声明,但是 仍然没有想到,我授权了。这是 App.razor 页面的问题吗?
    • 对不起,现在一切正常,我不知道发生了什么。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 2017-12-25
    • 2014-01-28
    相关资源
    最近更新 更多