aspnetcore: [Blazor WASM] Upgrading project to .NET 8 and breaks [Authorize] Attribute

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Whom it may concerned,

My project is upgrading to Net 8 and using @attribute [Authorize]. when I logged out and called NotifyAuthenticationStateChanged method by using CustomAuthenticationStateProvider and then it threw an exception, please see below:

Razor Page @attribute [Authorize] …

Code behind:

  • ClaimsPrincipal anonymous = new(new ClaimsIdentity());
  • NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(anonymous)));
  • return Task.CompletedTask;

Exception:

blazor.webassembly.js:1 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.<>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]].<NotifyChangedAsync>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)

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

NET 8

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 9
  • Comments: 28 (3 by maintainers)

Commits related to this issue

Most upvoted comments

I have the same issue. This is happening for me after I added builder.Services.AddCascadingAuthenticationState (.NET8) in the Program.cs. If I comment this line and switch back to using CascadingAuthenticationState in the App.razor then there is no issue. This happens when we logout and then immediately login back.

Can confirm, this seems to only happen while using AddCascadingAuthenticationState() instead of the CascadingAuthenticationState component.

The issue started occuring when I created a component in my layout that requested the cascading AuthState either from the CascadingParameter or the AuthorizeView.

@thepra, we’ll backport this if we get feedback that the .NET 9 fix fully addresses the problem. We’re still in the prerelease phase for .NET 9, but we want to be cautious about changes we include in .NET 8 patch releases because the impact of introducing a regression is higher there.

We’re very keen on getting feedback on this fix so that we can backport it. When .NET 9 RC1 releases, it would help us if you (and others) could confirm that it fixes the problem in your scenario.

Thanks for understanding!

@javiercn is there a way to stop auto-closing this issue (until it’s fixed)? I don’t quite understand this policy. Regardless of whether someone responds or not, you still want to fix this, and ensure that all other people who encountered it (and subscribed to this) are notified, right?

Pardone, why completed? The same problem. Is it fixed?

Ran into this issue myself. Can confirm that updating to 9 resolved it.

Same issue here with 8.0.6. Also fixed by reverting to <CascadingAuthenticationState>.

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.<>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]].<NotifyChangedAsync>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.6.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[<CompanyApplicationName>.Shared.Security.UserAccount, <CompanyApplicationName>.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[<CompanyApplicationName>.Oidc.Models.ExtendedOidcProviderOptions, <CompanyApplicationName>.Oidc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].UpdateUser(Task`1 task)
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService`3.<UpdateUserOnSuccess>d__31[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=8.0.6.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[<CompanyApplicationName>.Shared.Security.UserAccount, <CompanyApplicationName>.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[<CompanyApplicationName>.Oidc.Models.ExtendedOidcProviderOptions, <CompanyApplicationName>.Oidc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()

I also started getting this specific error with the same setup conditions (Blazor WASM on .NET 8). For us, it is only occurring on FTU authentication or logging out and in again. Refreshing the page after this error, and subsequent automatic re-authentication attempts do not exhibit the exception.

From the traces, and poking around at the source, it looks to be around the method CascadingValueSource.NotifyChangedAsync() :95 - :104:

foreach (var (dispatcher, subscribers) in _subscribers)
            {
                tasks.Add(dispatcher.InvokeAsync(() =>
                {
                    foreach (var subscriber in subscribers)
                    {
                        subscriber.NotifyCascadingValueChanged(ParameterViewLifetime.Unbound);
                    }
                }));
            }

Even though _subscribers itself has concurrency, the value is just a List<>.
It seems that list is being modified during the above foreach (:99) (aka additional subscribers being added to the dispatcher)?

It’s claimed the .Add is thread-safe, but perhaps for this specific condition it is not enough?

    void ICascadingValueSupplier.Subscribe(ComponentState subscriber, in CascadingParameterInfo parameterInfo)
    {
        Dispatcher dispatcher = subscriber.Renderer.Dispatcher;
        dispatcher.AssertAccess();

        // The .Add is threadsafe because we are in the sync context for this dispatcher
        _subscribers?.GetOrAdd(dispatcher, _ => new()).Add(subscriber);
    }

My complete stacktrace is:

An exception occurred:  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.<>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]].<NotifyChangedAsync>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.Authentication.WebAssembly.Msal.Models.MsalProviderOptions, Microsoft.Authentication.WebAssembly.Msal, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].UpdateUser(Task`1 task)
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService`3.<UpdateUserOnSuccess>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.Authentication.WebAssembly.Msal.Models.MsalProviderOptions, Microsoft.Authentication.WebAssembly.Msal, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService`3.<SignInAsync>d__20[[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.Authentication.WebAssembly.Msal.Models.MsalProviderOptions, Microsoft.Authentication.WebAssembly.Msal, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticatorViewCore`1.<ProcessLogIn>d__85[[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.<OnParametersSetAsync>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) 
 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.<>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]].<NotifyChangedAsync>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.Authentication.WebAssembly.Msal.Models.MsalProviderOptions, Microsoft.Authentication.WebAssembly.Msal, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].UpdateUser(Task`1 task)
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService`3.<UpdateUserOnSuccess>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.Authentication.WebAssembly.Msal.Models.MsalProviderOptions, Microsoft.Authentication.WebAssembly.Msal, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationService`3.<SignInAsync>d__20[[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.Authentication.WebAssembly.Msal.Models.MsalProviderOptions, Microsoft.Authentication.WebAssembly.Msal, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticatorViewCore`1.<ProcessLogIn>d__85[[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.<OnParametersSetAsync>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)

@thepra, the .NET 8 fix will be included in an upcoming servicing release (most likely in March).

It looks like NotifyAuthenticationStateChanged() causes the error if there is an AuthorizeView tag. I use AuthorizeView in my navbar, and removing the navbar while testing gets rid of the error. I see NotifyAuthenticationStateChanged accepts a task, but there is no option to invoke async. So I’m suspecting the rendering breaks because it is not awaited and the AuthorizeView breaks because the authorized state changes while rendering.

Chiming in here - we’ve just seen the same issue. Reverting to <CascadingAuthenticationState> in Routes.razor instead of using the service has solved the problem for now. This issue should definitely not be closed.

This problem also occurs in Blazor Server mode. Invoke of NotifyAuthenticationStateChanged throws System.Func`1 - “Collection was modified; enumeration operation may not execute” exception. I’ve also changed AddCascadingAuthenticationState service to <CascadingAuthenticationState> to fix this issue.