How to implement server side blazor Custom Authorization Provider Correctly

馋奶兔 提交于 2021-02-20 04:08:01

问题


I am trying to implement a basic custom Auth provider in my server side blazor project, but I am having some difficulty implementing the 'IsAuthenticating' property correctly.

I used this site as my starter point, but it throws an error if 'IsAuthenticating' is set to true (more specifically when I return null from GetAuthenticationStateAsync()). https://gunnarpeipman.com/client-side-blazor-authorizeview/.

Specifically this line of code:

if(IsAuthenticating)
    {
        return null; <---- This line throws error
    }
    else if(IsAuthenticated)
    {
        identity = new ClaimsIdentity(new List<Claim>
                    {
                        new Claim(ClaimTypes.Name, "TestUser")

                    }, "WebApiAuth");
    }
    else
    {
        identity = new ClaimsIdentity();
    }

The exact error thrown is:

NullReferenceException: Object reference not set to an instance of an object.
   Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync()

I did not want to clutter with stack trace.

Can this be achieved, and if so how?

来源:https://stackoverflow.com/questions/60624778/how-to-implement-server-side-blazor-custom-authorization-provider-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!