Skip to content

[Blazor WASM] Upgrading project to .NET 8 and breaks [Authorize] Attribute #53075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
patrickpham3339 opened this issue Jan 1, 2024 · 28 comments · Fixed by #57243
Closed
1 task done

[Blazor WASM] Upgrading project to .NET 8 and breaks [Authorize] Attribute #53075

patrickpham3339 opened this issue Jan 1, 2024 · 28 comments · Fixed by #57243
Assignees
Labels
area-blazor Includes: Blazor, Razor Components Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue
Milestone

Comments

@patrickpham3339
Copy link

patrickpham3339 commented Jan 1, 2024

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

@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Jan 1, 2024
@Ali1Jamal
Copy link

same problem, i use blazorWebView with windows forms and i have the same issue did you manage to fix it @patrickpham3339 ?

@uottoni
Copy link

uottoni commented Feb 4, 2024

Same here...
When I'm logginout the user i got this error too.
My custom AuthStateProvider:
var annonymousUser = new ClaimsPrincipal(new ClaimsIdentity());
var authState = Task.FromResult(new AuthenticationState(annonymousUser));
NotifyAuthenticationStateChanged(authState);
I got the error at last line: NotifyAuthenticationStateChanged(authState);
On my NET 7 project is working.

@patrickpham3339
Copy link
Author

patrickpham3339 commented Feb 4, 2024 via email

@mkArtakMSFT mkArtakMSFT added the Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue label Feb 6, 2024
@dotnet-policy-service dotnet-policy-service bot added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Feb 6, 2024
@ghost
Copy link

ghost commented Feb 6, 2024

Hi @patrickpham3339. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@JosephHedges
Copy link

JosephHedges commented Feb 6, 2024

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)

@bo-bac
Copy link

bo-bac commented Feb 22, 2024

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

@suntaurus
Copy link

suntaurus commented Mar 7, 2024

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.

@R-Vulcan
Copy link

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.

@hyspdrt
Copy link

hyspdrt commented Mar 28, 2024

I have the same issue, on logging in. I too switched to the AddCascadingAuthenticationState() and had to revert back.

not sure why this is closed...

@PeterWhatsUpp
Copy link

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 to fix this issue.

@mabster
Copy link

mabster commented Jun 19, 2024

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.

@igotinfected
Copy link

igotinfected commented Jun 24, 2024

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()

@hannesSwart9
Copy link

hannesSwart9 commented Jul 24, 2024

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.

@flier268
Copy link

flier268 commented Jul 31, 2024

So, I need to downgrade to <CascadingAuthenticationState>?

@Leon99
Copy link

Leon99 commented Aug 5, 2024

@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?

@MackinnonBuck MackinnonBuck added this to the 9.0-rc1 milestone Aug 9, 2024
@thepra
Copy link

thepra commented Aug 12, 2024

Ok, I'm sorry @MackinnonBuck for bothering but this is bullshit.
Solving this issue in an upcoming STS(.NET 9) release when it was first introduced(with .AddCascadingAuthenticationState()) in the currently supported LTS(.NET 8) release is kind of fucked up, sincerely.
immagine

Forget about the wordaround, what kind of LTS is this when you introduce a new capability within this release, then find out that it does not work as intended and have it fixed in the next damn release?
Someone at Microsoft can answer if this is expected as a default treatment of developers using the framework going forward or it can be fixed within this LTS release.

@MackinnonBuck
Copy link
Member

@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!

@thepra
Copy link

thepra commented Aug 13, 2024

Sounds good, lets wait.

@hyspdrt
Copy link

hyspdrt commented Aug 15, 2024

Updated to 8.0.8 with the patched code, and if I nest an AuthorizeView, I still encounter the exception. If I revert back to the <CascadingAuthenticationState>, nesting works just fine.

But, if I don't nest and only have 1 AuthorizeView, it does work and does not throw the error. So, it seems it's partially resolved.

Also, I'm using WASM Standalone, so the Dispatcher is the NullDispatcher. And the AuthorizeView is used in my Header.razor component which is on the MainLayout.razor, but the nested AuthorizeView is only used in the Authorized fragment of the parent AuthorizeView, so it only throws upon successful login.

Hope this feedback is helpful.

@janhjordie
Copy link

janhjordie commented Oct 20, 2024

Same issue in dotnet 8.0.10. Just wasted several hours on figuring how to fix it. Please reopen !!!!!!!!!!!

@mbrannen
Copy link

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

@suntaurus
Copy link

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

If .net9 has the fix, then it's really a great news :)

@Poffertjie
Copy link

Poffertjie commented Nov 27, 2024

This is not just related to WASM but Server as well. How is this closed? Problem remains in .net 8 and forcing us to upgrade to .net 9 is just wrong.

program.cs

builder.Services.AddCascadingAuthenticationState();

Then I have CustomAuthStateProvider which calls NotifyAuthenticationStateChanged when a customer logs in or out.

The above throws the exception

2024-11-27T09:19:44.260Z] Error: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collections.Generic.List1.Enumerator.MoveNext()
at Microsoft.AspNetCore.Components.CascadingValueSource1.<>c__DisplayClass10_0.<NotifyChangedAsync>b__0() at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContextDispatcher.InvokeAsync(Action workItem) at Microsoft.AspNetCore.Components.CascadingValueSource1.NotifyChangedAsync()
at Microsoft.Extensions.DependencyInjection.CascadingAuthenticationStateServiceCollectionExtensions.AuthenticationStateCascadingValueSource.HandleAuthenticationStateChanged(Task`1 newAuthStateTask)

removing this line

builder.Services.AddCascadingAuthenticationState();

and reverting to

<CascadingAuthenticationState>

Resolved the problem.

@Conman-123
Copy link

A janky temporary solution for me was to add a handler for the OnNavigateAsync parameter of my Router in App.razor:

<Router AppAssembly="@typeof(App).Assembly" OnNavigateAsync="HandleNavigateStarted">

...

private void HandleNavigateStarted(NavigationContext context)
{
    StateHasChanged(); // This part isn't important, just having *something* here seems to fix it
}

Syntrice added a commit to nc-team-inertia/spreeview that referenced this issue Feb 3, 2025
The AddCascadingAuthenticationState doesn't work properly on dotnet 8.0,
despite the docs indicating so. See
dotnet/aspnetcore#53075
@thepra
Copy link

thepra commented Feb 4, 2025

@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!

@MackinnonBuck Is there any news?

@MackinnonBuck
Copy link
Member

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

@VJayMeyer
Copy link

Any updates on the lead time? I can see https://learn.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-8.0&tabs=visual-studio#troubleshoot-errors saying not to use to rap the but it's still the only solution.

@DanielCordell
Copy link
Contributor

DanielCordell commented Mar 11, 2025

Any updates on the lead time? I can see https://learn.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-8.0&tabs=visual-studio#troubleshoot-errors saying not to use to rap the but it's still the only solution.

@VJayMeyer being hopeful, looking at the MR to backport this change, #57288
it was merged last month and has the milestone 8.0.14 on it, which appears to have just come out? May already be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue
Projects
None yet