#SharePointProblems | Koskila.net

Solutions are worthless unless shared! Antti K. Koskela's Personal Professional Blog

Fixing Blazor WebAssembly .NET 6 MSAL authorization bug

koskila
Reading Time 3 min
Word Count 387 words
Comments 6 comments
Rating 5 (5 votes)
View

Let's make this short and sweet. In this particular case, upgrading a Blazor WASM solution to .NET 6 broke the authentication when deployed to Azure. The same issue could probably happen to a fresh project, though.

This is what you're running into: If you open your browser console, you'll see 2 interesting errors - first:

dotnet.6.0.0.o2we6pverp.js:1 mono_wasm_runtime_ready login.microsoftonline.com/common/oauth2/v2.0/token:1 Failed to load resource: the server responded with a status of 400 (Bad Request)

And a moment later, an error along the lines of:

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Cannot read properties of undefined (reading 'toLowerCase')
TypeError: Cannot read properties of undefined (reading 'toLowerCase')
at u.signInCore (/_content/Microsoft.Authentication.WebAssembly.Msal/AuthenticationService.js:2:193591)
at u.getUser (/_content/Microsoft.Authentication.WebAssembly.Msal/AuthenticationService.js:2:191904)
Microsoft.JSInterop.JSException: Cannot read properties of undefined (reading 'toLowerCase')
TypeError: Cannot read properties of undefined (reading 'toLowerCase')

And perhaps the most annoying is that it only happens in Azure! Locally, everything runs just fine. Another one of those situations when it's not enough that it runs fine on your machine...

The first error might also contain some nonsense about your app not existing in the tenant, "perhaps you've sent your login request to the wrong tenant" and all that - but we know that's not the case. Although... On a closer look, the tenant id in the error shouldn't seem familiar at all.

Reason

Microsoft forgot to fix a bug in their WebAssembly package size optimizations - Assembly Trimming, namely - and that causes the authentication library to get confused between different tenants, and finally mess up the authentication altogether.

Funnily enough, this has been on their radar at least since .NET 6 Preview 4, as reported in June (see the GitHub link at the end of the article) - but sometimes even the "no-brainer"-looking issues won't get fixed in time for GA/RTM, for a multitude of reasons.

Anyway, this is how you fix it:

Solution

Time needed: 5 minutes.

How to fix Blazor WebAssembly 400 error when authenticating (on .NET 6)?

  1. Edit your Client-project's .csproj -file

    Just click on the project in Solution Explorer with the right mouse button and select "Edit project file":

    "Edit Project File" in Visual Studio Solution Explorer

  2. Add an exclusion for

    Add this as a new ItemGroup: <ItemGroup> <TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" /> </ItemGroup>

  3. Remove your "bin" & "obj" folders and rebuild

    You should be good!

That should be it. Happy Blazing! 😊

References

Comments

Interactive comments not implemented yet. Showing legacy comments migrated from WordPress.
Don
2021-12-13 14:37:48)
Thank you so much! Very interested in how you went about finding this! I had no way knowing where to start with such an unhelpful exception message.
Antti K. Koskela
2021-12-17 23:04:40
My pleasure, Don! Truth be told, I picked it up from the GitHub discussion after a period of frantic googling. Thought it needs documenting, so people find it easier... 😅
Dug Steen
2022-01-16 02:07:26)
Wow. You can't imagine how much pain I've been through to get this fixed. I owe you a coffee or beer or something. THANK YOU
Antti K. Koskela
2022-01-22 22:05:54
Thanks for the kind words, Dug - all in a day's work! 😅
2022-08-30 13:51:47)
Just hit similar error (this._settings.loginMode is undefined) with same resolution and it's almost September 2022. Many thanks for your post - saved the day!
Antti K. Koskela
2023-03-03 20:53:50
Hi Rob, Thanks for the comment - happy to hear the article was helpful, even if it's a bit odd it was! 😅
Whitewater Magpie Ltd.
© 2025
Static Site Generation timestamp: 2025-08-12T07:18:38Z