dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.37k stars 9.99k forks source link

<Authorizing> state cannot be triggered in Blazor WASM without throwing an exception #32464

Closed AdamDiament closed 3 years ago

AdamDiament commented 3 years ago

.net5 blazor wasm template project, https

Given this flow:

        <AuthorizeView>
            <Authorized>
                Welcome @context.User.FindFirst(ClaimTypes.GivenName)?.Value
                <NavLink class="nav-link" href="logout">
                    <span class="oi oi-account-logout"></span> Log Out
                </NavLink>
            </Authorized>
            <NotAuthorized>
                <Login/>
            </NotAuthorized>
            <Authorizing>
                <text>Please wait the system is authorizing your credentials</text>
            </Authorizing>
        </AuthorizeView>

In my subclassed auth state provider, I am doing this

//logged in successfully
NotifyAuthenticationStateChanged(Task.FromResult(new ClaimsPrincipal(
        new ClaimsIdentity(JwtParser.ParseClaimsFromJWT(token),
          "jwtAuthType"))));
//logged out
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()))));

They both work fine. Now I want to set the state to "Authorizing". What do I do?

The official documentation mentions the Authorizing state as a valid state, but not explicitly how to trigger it.

Doing this:

//starting to log in
NotifyAuthenticationStateChanged(Task.FromResult<AuthenticationState>(null));

does indeed trigger the "Authorizing" state in the UI, so a "null" AuthenticationState does seem to be what blazor is expecting. However, it throws an exception on the front end even though it works as expected otherwise. Passing any kind of ClaimsPrincipal, causes either "Authorized" or "NotAuthorized", but never "Authorizing".

This is the exception triggered for that attempt

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
d.printErr  @   blazor.webassembly.js:1
d.preRun.push.window.Blazor._internal.dotNetCriticalError   @   blazor.webassembly.js:1
w   @   blazor.webassembly.js:1
_mono_wasm_invoke_js_blazor @   dotnet.5.0.5.js:1
do_icall    @   00aba242:0x194e4e
do_icall_wrapper    @   00aba242:0x79df9
interp_exec_method  @   00aba242:0x44ad3
interp_runtime_invoke   @   00aba242:0x12efff
mono_jit_runtime_invoke @   00aba242:0x118e5f
do_runtime_invoke   @   00aba242:0x79d42
mono_runtime_invoke_checked @   00aba242:0xf65d
mono_runtime_try_invoke_array   @   00aba242:0x10e831
ves_icall_InternalInvoke    @   00aba242:0xed492
ves_icall_InternalInvoke_raw    @   00aba242:0xecf57
do_icall    @   00aba242:0x194ddb
do_icall_wrapper    @   00aba242:0x79df9
interp_exec_method  @   00aba242:0x44ad3
interp_runtime_invoke   @   00aba242:0x12efff
mono_jit_runtime_invoke @   00aba242:0x118e5f
do_runtime_invoke   @   00aba242:0x79d42
mono_runtime_try_invoke @   00aba242:0x12982
mono_runtime_invoke @   00aba242:0x10ec2b
mono_wasm_invoke_method @   00aba242:0x108e48
Module._mono_wasm_invoke_method @   dotnet.5.0.5.js:1
call_method @   dotnet.5.0.5.js:1
(anonymous) @   dotnet.5.0.5.js:1
beginInvokeDotNetFromJS @   blazor.webassembly.js:1
h   @   blazor.webassembly.js:1
e.invokeMethodAsync @   blazor.webassembly.js:1
(anonymous) @   blazor.webassembly.js:1
invokeWhenHeapUnlocked  @   blazor.webassembly.js:1
(anonymous) @   blazor.webassembly.js:1
t.dispatchEvent @   blazor.webassembly.js:1
(anonymous) @   blazor.webassembly.js:1
(anonymous) @   blazor.webassembly.js:1
e.onGlobalEvent

I also tried

      var authenticatingStateTask = Task.FromResult(new AuthenticationState(null));
      NotifyAuthenticationStateChanged(authenticatingStateTask);

This time it doesn't even trigger the state, and a different exception is encountered.

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Value cannot be null. (Parameter 'user')
System.ArgumentNullException: Value cannot be null. (Parameter 'user')
   at Microsoft.AspNetCore.Components.Authorization.AuthenticationState..ctor(ClaimsPrincipal user)
   at VisualService.Portal.Authentication.PortalAuthStateProvider.NotifyUserAuthenticating() in C:\projects\RemoteMonitoringNEW\EverythingIsInHere\SourceCode\ClientApps\VisualService.Portal\Authentication\PortalAuthStateProvider.cs:line 42
   at VisualService.Portal.Authentication.AuthenticationService.Login(String username, String password) in C:\projects\RemoteMonitoringNEW\EverythingIsInHere\SourceCode\ClientApps\VisualService.Portal\Authentication\AuthenticationService.cs:line 41
   at VisualService.Portal.Pages.Login.ExecuteLogin() in C:\projects\RemoteMonitoringNEW\EverythingIsInHere\SourceCode\ClientApps\VisualService.Portal\Pages\Login.razor:line 32
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
d.printErr @ blazor.webassembly.js:1
d.preRun.push.window.Blazor._internal.dotNetCriticalError @ blazor.webassembly.js:1
w @ blazor.webassembly.js:1
_mono_wasm_invoke_js_blazor @ dotnet.5.0.5.js:1
do_icall @ 00aba242:0x194e4e
do_icall_wrapper @ 00aba242:0x79df9
interp_exec_method @ 00aba242:0x44ad3
interp_runtime_invoke @ 00aba242:0x12efff
mono_jit_runtime_invoke @ 00aba242:0x118e5f
do_runtime_invoke @ 00aba242:0x79d42
mono_runtime_invoke_checked @ 00aba242:0xf65d
mono_runtime_try_invoke_array @ 00aba242:0x10e831
ves_icall_InternalInvoke @ 00aba242:0xed492
ves_icall_InternalInvoke_raw @ 00aba242:0xecf57
do_icall @ 00aba242:0x194ddb
do_icall_wrapper @ 00aba242:0x79df9
interp_exec_method @ 00aba242:0x44ad3
interp_runtime_invoke @ 00aba242:0x12efff
mono_jit_runtime_invoke @ 00aba242:0x118e5f
do_runtime_invoke @ 00aba242:0x79d42
mono_runtime_try_invoke @ 00aba242:0x12982
mono_runtime_invoke @ 00aba242:0x10ec2b
mono_wasm_invoke_method @ 00aba242:0x108e48
Module._mono_wasm_invoke_method @ dotnet.5.0.5.js:1
call_method @ dotnet.5.0.5.js:1
(anonymous) @ dotnet.5.0.5.js:1
beginInvokeDotNetFromJS @ blazor.webassembly.js:1
h @ blazor.webassembly.js:1
e.invokeMethodAsync @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
invokeWhenHeapUnlocked @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
t.dispatchEvent @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
(anonymous) @ blazor.webassembly.js:1
e.onGlobalEvent @ blazor.webassembly.js:1
Show 6 more frames
ghost commented 3 years ago

Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!

AdamDiament commented 3 years ago

@javiercn @mkArtakMSFT Hi both - I think the bot has closed this erroneously - this is still an issue