【发布时间】:2021-07-28 19:18:30
【问题描述】:
鉴于此流程:
<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>
在我的子类身份验证状态提供程序中,我正在这样做
//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()))));
它们都工作正常。现在我想将状态设置为“授权”。我该怎么办?
official documentation 提到授权状态是有效状态,但没有明确说明如何触发它。
这样做:
//starting to log in
NotifyAuthenticationStateChanged(Task.FromResult<AuthenticationState>(null));
确实会触发 UI 中的“授权”状态,因此“空”AuthenticationState 似乎确实是 blazor 所期望的。但是,即使它按预期工作,它也会在前端引发异常。传递任何类型的 ClaimsPrincipal 会导致“已授权”或“未授权”,但绝不会“授权”。
这是该尝试触发的异常
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
我也试过
var authenticatingStateTask = Task.FromResult(new AuthenticationState(null));
NotifyAuthenticationStateChanged(authenticatingStateTask);
这一次甚至没有触发状态,遇到了不同的异常。
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
【问题讨论】:
标签: c# authentication .net-5 blazor-webassembly