aspnetcore: Blazor WASM Net 6 Preview 4 Azure AD - There was an error trying to log you in: 'Cannot read property 'toLowerCase' of undefined'

I have a simple Blazor WASM running Net 6 Preview 4 that I setup using this guide:

https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/hosted-with-azure-active-directory?view=aspnetcore-6.0

The exact code works perfect with Net 5.0 (using corresponding Net 5.0 assemblies)

However when I upgrade to Net 6 I receive the following Authentication error:

eod4H

There was an error trying to log you in: 'Cannot read property 'toLowerCase' of undefined'

I can’t seem to get anymore detail either. This is published on Azure App Service Self Contained publish.

The code can be found here: https://github.com/aherrick/BlazorWASM.Net6Prev4.AzureAD

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 55 (5 by maintainers)

Commits related to this issue

Most upvoted comments

The issue is caused by assembly trimming done in published version (the property LoginMode in MsalProviderOptions gets removed).

To workaround it exclude Microsoft.Authentication.WebAssembly.Msal from trimming by adding this to the client project file (tester.Client.csproj):

  <ItemGroup>
    <TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" />
  </ItemGroup>

I was having this problem as well on .NET 6.0.1 using the latest version of the MSAL library. The only problem was that I wasn’t even getting any kind of error message. I would only get an empty string. Adding in the TrimRootAssembly node in my project files resolved this issue for me in multiple projects.

This issue has been open since June of 2021 and as of this writing it is February 2022. Does this mean that everyone who runs .NET 6 Blazor WASM and tries to do a Release build is having this problem? Shouldn’t something like this have been fixed before .NET 6 went to RTM? Even more so, shouldn’t it have been prioritized for the 6.0.1 release?

Or will this become part of the standard project file template if you decide to use MSAL when creating a new project?

Delete all bin and obj folders from the solution, delete all the files in destination folder on the server before publishing again.

Is it possible to add <TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" /> to the project template so folks don’t run into this?

We believe this has been addressed in 7.0 as part of https://github.com/dotnet/aspnetcore/pull/43954.

The fix will be available in RC2.

I was having this problem as well on .NET 6.0.2 and VS2022. Publish to folder, FTP to server; site runs but log-in fails with: There was an error trying to log you in: 'Cannot read properties of undefined (reading 'toLowerCase')'

Added this line, closed VS, and deleted my bin/obj folders then republished and it worked.

<ItemGroup> 
     <TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" />
</ItemGroup>

I had the same issue when trying to sign in. Updating Microsoft.Identity.Web and Microsoft.Identity.Web.UI to latest version(1.22.1) and then deleting the bin and obj folders solved the problem for me.

There was no need to add an ItemGroup with TrimmerRootAssembly.

@parko65 @Mr-Technician Updating to VS2022 17.0.2 has fixed the issue. Can now publish in Release mode and it doesn’t throw the integrity issue. Thanks!

@Mr-Technician and @parko65 It worked!!! Thanks!!!

This is still an issue in VS2022. I’ve looked around and have found no answer. Just using the base template has this issue. I’ve tried all ideas with no luck. Is there a change in what’s needed on the Azure app registration side? Any help would be appreciated.

image

Thanks!!

I have an ASP.NET hosted Blazor WASM project on .NET 6 RC2. I am using Azure B2C for authentication. Everything works fine locally and also deployed to Azure as long as I have the “Optimize code” setting unchecked for the Release profile.

I added the above TrimmerRootAssembly include to my client project file and published to an Azure app service. I am still getting this message when attempting to login. I checked the wwwroot folder and the Microsoft.Authentication.WebAssembly.Msal assembly is there. I also removed all of the contents of the wwwroot folder and published again. I have tried both self-contained and framework-dependent deployments. I also added the redirect URLs to my B2C apps that point to my Azure app service.

Edit: Debugging AuthenticationService.js shows that the loginMode is undefined. I am setting LoginMode in my Program.cs file to “redirect”.

@Mr-Technician the console is clean and no errors and warnings are logged.

@stanbeamish thanks for the suggestion. I will try this as soon as I arrive home and let you know if it worked for me.

I ran into the same problem myself and wanted to create a bug with my temporary solution. And it was only now that I found that it has already been reported.

The temporary solution that I made is similar to the one reported here but a bit different. I also noticed that publish operation is trimming some of the MSIL-related classes. However, I didn’t want to fully prevent the trimming for the entire Microsoft.Authentication.WebAssembly.Msal assembly but only for the involved classes.

So, I added this in the Blazor .csproj file:

<ItemGroup>
	<TrimmerRootDescriptor Include="TrimmerDescriptors.xml" />
</ItemGroup>

Additionally, I added the TrimmerDescriptors.xml file with the following content:

<?xml version="1.0" encoding="utf-8" ?>
<linker>
  <assembly fullname="Microsoft.Authentication.WebAssembly.Msal">
    <type fullname="Microsoft.Authentication.WebAssembly.Msal.Models.MsalProviderOptions" preserve="all" />
    <type fullname="Microsoft.Authentication.WebAssembly.Msal.Models.MsalCacheOptions" preserve="all" />
    <type fullname="Microsoft.Authentication.WebAssembly.Msal.MsalAuthenticationOptions" preserve="all" />
  </assembly>
</linker>

For me, only those 3 classes represented the problem so I forced the trimmer to fully preserve them while it can safely trim the rest of the assembly.

Visual Studio 17.1.0 New WebAssembly Blazor site with Windows Identity Provider option. Unmodified except to adding config (works in Dev fine), added trim to csproj and updated all packages to latest:

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

	<PropertyGroup>
		<TargetFramework>net6.0</TargetFramework>
		<Nullable>enable</Nullable>
		<ImplicitUsings>disable</ImplicitUsings>
		<ServiceWorkerAssetsManifest>service-worker-assets.js</ServiceWorkerAssetsManifest>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.2" />
		<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.2" PrivateAssets="all" />
		<PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="6.0.2" />
	</ItemGroup>

	<ItemGroup>
		<TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" />
	</ItemGroup>

	<ItemGroup>
		<ServiceWorker Include="wwwroot\service-worker.js" PublishedContent="wwwroot\service-worker.published.js" />
	</ItemGroup>

</Project>

Publish to folder, FTP to server; site runs but log-in fails with: There was an error trying to log you in: 'Cannot read properties of undefined (reading 'toLowerCase')'

I’ve done the delete bin/obj and cleared target before deploy. Have I missed anything?

Update: Fired up a different box that only has VS2022 17.0.5 on it (original had VS2019 in parallel) and tried both Debug and Release. All same; fine running local but error deployed.

Update 20220220: Using the aggregate GitHub action “Build and Deploy” which uses Oryx to build it generates a site from same Git commit that, when deployed - works. I really don’t want to have to use dark-magic aggregate tasks like this though. So still stuck; can’t deploy by dotnet publish -> ftp to server.

Update 20220220b: Using the CLI instead of Visual Studio yields a publish folder that works. dotnet publish $projectRoot$p_ProjectName.csproj -p:BlazorEnableCompression=true -p:configuration=Release dotnet publish $projectRoot$p_ProjectName.csproj -p:BlazorEnableCompression=false -p:configuration=Release Interestingly Compressed=47.6MB Uncompressed=31.1MB. Presumably on larger sites there would be a benefit but on this smallest possible template site seems like you’re better off without.

@crbrz Thanks for the proposed workaround. It fixed the issue on my release docker build. Your answer sounded also like you know a potential root cause.

@mkArtakMSFT is this still planned in for any future sprint?

So, I had to do the whole thing below, on both client and server project… <ItemGroup> <TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" /> </ItemGroup>

As well as this on manual publish image

I was performing the build on an AzD build server (on-premises) and had to RDP to that and delete the obj and bin folders from the temporary build directory, then run the Build and Release pipelines again to resolve this error.

Path for me was: <Installation folder of agent>\_work\<numbered build folder e.g. 165>\s\<AzD Project name>\<C# Project name>\obj

Hope that helps someone else.

The issue still exists even after I upgraded to VS 2022 17.0.2. Applied the workaround suggested by @crbrz, the issue is gone.

@crbrz thanks! is there more to it? I add that line and then receive the following error when publishing. 6-24-2021 10-45-32 AM