You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8迁移Blazor WASM托管项目移除CascadingAuthenticationState后登录异常

Blazor WebAssembly .NET 8 OIDC登录完成后无法重定向异常问题

我有一个.NET Core托管的Blazor WebAssembly项目,使用外部企业服务器的OIDC认证,认证配置代码如下:

builder.Services.AddOidcAuthentication(opt =>
{
    opt.ProviderOptions.Authority = "https://xx.xx.pl/auth/realms/xx";
    opt.ProviderOptions.ClientId = "xxxx";
    opt.ProviderOptions.DefaultScopes.Add("email");
    opt.ProviderOptions.ResponseType = "code";
});

按照.NET 7到8迁移文档操作,在.NET 8中尝试移除<CascadingAuthenticationState>组件:

  • 添加builder.Services.AddCascadingAuthenticationState();时,功能正常,跳转逻辑无问题;
  • 移除<CascadingAuthenticationState>组件后,运行时显示未授权(符合预期),能正常跳转到外部登录页完成登录,但返回项目页面后一直显示“Authentication completing login...”,无法重定向到原页面,同时抛出以下异常:
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]   
Unhandled exception rendering component: Collection was modified; enumeration operation may not execute.  
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

   at System.Collections.Generic.List`1.Enumerator[[Microsoft.AspNetCore.Components.Rendering.ComponentState, Microsoft.AspNetCore.Components, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNextRare()
   at System.Collections.Generic.List`1.Enumerator[[Microsoft.AspNetCore.Components.Rendering.ComponentState, Microsoft.AspNetCore.Components, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
   at Microsoft.AspNetCore.Components.CascadingValueSource`1.&lt;&gt;c__DisplayClass10_0[[System.Threading.Tasks.Task`1[[Microsoft.AspNetCore.Components.Authorization.AuthenticationState, Microsoft.AspNetCore.Components.Authorization, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]], System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].&lt;NotifyChangedAsync&gt;b__0()
   at Microsoft.AspNetCore.Components.WebAssembly.Rendering.NullDispatcher.InvokeAsync(Action workItem)
   at Microsoft.AspNetCore.Components.CascadingValueSource`1[[System.Threading.Tasks.Task`1[[Microsoft.AspNetCore.Components.Authorization.AuthenticationState, Microsoft.AspNetCore.Components.Authorization, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]], System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].NotifyChangedAsync()
   at Microsoft.AspNetCore.Components.CascadingValueSource`1[[System.Threading.Tasks.Task`1[[Microsoft.AspNetCore.Components.Authorization.AuthenticationState, Microsoft.AspNetCore.Components.Authorization, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]], System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].NotifyChangedAsync(Task`1 newValue)
   at Microsoft.Extensions.DependencyInjection.CascadingAuthenticationStateServiceCollectionExtensions.AuthenticationStateCascadingValueSource.HandleAuthenticationStateChanged(Task`1 newAuthStateTask)
   at Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider.NotifyAuthenticationStateChanged(Task`1 task)
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService`3[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].UpdateUser(Task`1 task)
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService`3.&lt;UpdateUserOnSuccess&gt;d__30[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService`3.&lt;CompleteSignInAsync&gt;d__21[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.OidcProviderOptions, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticatorViewCore`1.&lt;ProcessLogInCallback&gt;d__86[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticatorViewCore`1.&lt;OnParametersSetAsync&gt;d__84[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

目前已添加以下服务配置:

builder.Services.AddOptions();
builder.Services.AddAuthorizationCore();

请问我哪里操作错误?


内容的提问来源于stack exchange,提问作者Dorian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 23:34:50