dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.4k stars 10k forks source link

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

Closed aherrick closed 2 years ago

aherrick commented 3 years ago

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

mkArtakMSFT commented 3 years ago

Thanks for contacting us. Could you please share a repro, which is using a clean .NET 6 Preview 4 app (not upgraded from an earlier version). If that still reproduces, we'll take a look.

aherrick commented 3 years ago

@mkArtakMSFT yep its linked here. Thanks!

https://github.com/aherrick/BlazorWASM.Net6Prev4.AzureAD

ghost commented 3 years ago

Thanks for contacting us. We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. Because it's not immediately obvious that this is a bug in our framework, we would like to keep this around to collect more feedback, which can later help us determine the impact of it. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

crbrz commented 3 years ago

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>
aherrick commented 3 years ago

@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

crbrz commented 3 years ago

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

aherrick commented 3 years ago

@crbrz that worked! thank you!

ScottyLightnin commented 3 years ago

Worked for me too, thank you.

anthonychu commented 3 years ago

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

joshgreenwald commented 3 years ago

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".

ghost commented 3 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

bryanp-ramosoil commented 2 years ago

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

bryanp-ramosoil commented 2 years ago

FYI: The mentioned workaround doesn't work in the latest version.

2021-11-19 14_50_42-RamosOil Intranet

parko65 commented 2 years ago

FYI: The mentioned workaround doesn't work in the latest version.

2021-11-19 14_50_42-RamosOil Intranet

bryanp-ramosoil You haven't got VS2022 installed alongside vs2019 by any chance?

nafi-haider commented 2 years ago

@parko65 I have the same problem, I tried all the solutions, image

bryanp-ramosoil commented 2 years ago

I uninstalled VS2019 prior to installing VS2022. I was wondering if I should reinstall VS2019. Based on your issue it looks as if that wouldn't work.

Trapulo commented 2 years ago

I have a project upgraded from 5.0 to 6.0. All ok in debug, but when the app is published I am not able to perform a login. The login controller does NOT redirect users to Azure, and reports only the "RemoteAuthenticatorView.LogInFailed" template.

I have NOT in console the reported errors (in fact I haven't any error, only the user authenticated requirement failure), but using the suggested workaround I was able to publish a working app.

nafi-haider commented 2 years ago

I also upgraded the project from 5.0 to 6.0, and got error There was an error trying to log you in: 'Cannot read property 'toLowerCase' of undefined', so I created new project using msidentity template (and by using VS > New Project) and published again, still same error, I then tried suggestion by @crbrz still no fix.

parko65 commented 2 years ago

@tiputrading I had same issue with 6.0, had another machine with VS2019 .Net5.0, built new Hosted BlazorWasm sln with MSIdentity using dotnet CLI - see sample in docs link. Configured AAD as per docs, published to remote IIS Server and all works ok.

VultureJD commented 2 years ago

Any update on this? Using the 6.0 template and following the instructions and then the steps here, I'm still getting the integrity failing: Failed to find a valid digest in the 'integrity' attribute for resource 'https://xxxxxxxxxxxxxx.azurewebsites.net/_framework/Microsoft.Authentication.WebAssembly.Msal.dll' with computed SHA-256 integrity '7G9gFCp+PjD9I0hpityl8yAr1zyzNL+tdrS3MpMHvl4='. The resource has been blocked.

parko65 commented 2 years ago

Any update on this? Using the 6.0 template and following the instructions and then the steps here, I'm still getting the integrity failing: Failed to find a valid digest in the 'integrity' attribute for resource 'https://xxxxxxxxxxxxxx.azurewebsites.net/_framework/Microsoft.Authentication.WebAssembly.Msal.dll' with computed SHA-256 integrity '7G9gFCp+PjD9I0hpityl8yAr1zyzNL+tdrS3MpMHvl4='. The resource has been blocked.

Have you got VS2019 installed alongside VS2022?

VultureJD commented 2 years ago

@parko65 I do have both VS2019 and VS2022. I'm publishing directly from VS2022 into the App Service. If I publish in Debug, it all works fine, but Release throws the error above even when excluded from trimming.

bryanp-ramosoil commented 2 years ago

I tried publishing in debug to IIS and I'm still getting the following error.

There was an error trying to log you in: 'Cannot read properties of undefined (reading 'toLowerCase')'

This is getting frustrating

parko65 commented 2 years ago

I tried publishing in debug to IIS and I'm still getting the following error.

There was an error trying to log you in: 'Cannot read properties of undefined (reading 'toLowerCase')'

This is getting frustrating

@bryanp-ramosoil Are you publishing from VS2022? I had this problem also when both versions of VS were installed. I uninstalled 2019 and all is good. I'm publishing to a commercially hosted IIS, using .NET5 (if .NET6 I have to publish as self contained as hosting service doesn't support .NET6 as yet). I'll try and replicate your problem and let you know.

bryanp-ramosoil commented 2 years ago

@parko65 I've tried publishing in both VS2022 and VSCode. Results are the same. In both cases I published with the debug profile.

Used this in VSCode : dotnet publish -c Debug -o ./publish

Mr-Technician commented 2 years ago

I just ran into this issue and the below fixed it. I'm running the latest VS 2022 and the latest .NET 6.

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

parko65 commented 2 years ago

@bryanp-ramosoil replicated your issue, created new blazorwasm using CLI using https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/hosted-with-azure-active-directory?view=aspnetcore-6.0. Used @crbrz recommendations re trimming and deleting obj and bin subdirectories. Re published and all ok.

bryanp-ramosoil commented 2 years ago

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

VultureJD commented 2 years ago

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

chgong commented 2 years ago

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

preservedmoose commented 2 years ago

Same for me... I added this to the Blazor Client application settings

image

Then deleted all of the bin and obj folders in the solution. I then stopped the site in Azure and published the solution. I did not need to delete any folders on Azure... After the publish completed, I started the site again and it is now working. I had previously tried a Clean Solution / Rebuild Solution but it did not work - you have to manually delete the bin and obj folders.

FrankAuwers commented 2 years ago

I don't have this problem with .NET 6 on my dev machine, but when I deploy it to an Azure App Service, I get the same error.

zzollner commented 2 years ago

The location <TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" /> is added in the .csproj file does matter.

Adding it to the end does not work. Placing it directly after your PackageReference item group seems to work.

FrankAuwers commented 2 years ago

I have alredy did it this way, but when it is hosted on Azure I will receive the same error.

net6.0 enable enable service-worker-assets.js
dstretch commented 2 years ago

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.

mkb13 commented 2 years ago

Same for me... I added this to the Blazor Client application settings

image

Then deleted all of the bin and obj folders in the solution. I then stopped the site in Azure and published the solution. I did not need to delete any folders on Azure... After the publish completed, I started the site again and it is now working. I had previously tried a Clean Solution / Rebuild Solution but it did not work - you have to manually delete the bin and obj folders.

How do you stop the site in Azure? I am using Azure Static Web Apps and I am not sure how to access the files to delete them nor how to stop the site (which I guess wouldn't be stopped, technically, because it's static?). I am publishing via github. This problem is seriously annoying me -- I have had so many issues trying to get things to work in Blazor Web Assembly that don't seem to quite work right and it doesn't seem to be getting a ton of attention. The TrimmerRootAssembly workaround caused a different error on the server (works fine locally) and turning off optimization for release had no effect.

preservedmoose commented 2 years ago

@mkb13 go to App Services, select your application, Overview in the sidebar and you can stop and start the site there. image

dstretch commented 2 years ago

They're using Static Web Sites, so no App Service. As long as you've overwritten the files it should be fine.

The bits you need to delete (obj & bin folders) are on your build server/machine.

On Thu, 23 Dec 2021, 18:35 Stephen Cousins, @.***> wrote:

@mkb13 https://github.com/mkb13 go to App Services, select your application, Overview in the sidebar and you can stop and start the site there. [image: image] https://user-images.githubusercontent.com/26547539/147279814-362cd682-2c30-4e64-8beb-5ae9864c16d7.png

— Reply to this email directly, view it on GitHub https://github.com/dotnet/aspnetcore/issues/33312#issuecomment-1000468619, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTZAJPGNNAK4N6PBW5A74DUSNTXNANCNFSM46ERM5UA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

130nk3r5 commented 2 years ago

So, I had to do the whole thing below, on both client and server project... `

`

As well as this on manual publish image

JonathanKrantz99 commented 2 years ago

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.

markusrt commented 2 years ago

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

stephajn commented 2 years ago

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?

9swampy commented 2 years ago

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.

Mason742 commented 2 years ago

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>
mwasson74 commented 2 years ago

FYI for anyone else with this issue:

IvanFeric commented 2 years ago

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.

lnaie commented 2 years ago

I had the same problem while migrating from v 5.0 to 6.04 and this one fixed it, also while hosting it into a docker image built up with the Azure DevOps CI. Thanks!

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>
jing8956 commented 2 years ago

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

@crbrz Thanks, I added DynamicDependencyAttribute to the main function and it worked as well, proving you were right.

static class Program
{
    [DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, typeof(MsalProviderOptions))]
    [DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties, typeof(MsalAuthenticationOptions))]
    static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);

        builder.RootComponents.Add<App>("#app");
        builder.RootComponents.Add<HeadOutlet>("head::after");

        builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

        builder.Services.AddMsalAuthentication(options =>
        {
            builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
        });

        await builder.Build().RunAsync();
    }
}
jt-pt-dev commented 2 years ago

I encountered this issue still today.

A new Blazor WASM .NET hosted solution using AAD B2C for authentication. Straight from the Visual Studio template (VS 17.3.0), built using GitHub Actions and deployed to Azure App Service. Works locally but not when deployed. Gives the error below when navigating to /fetchdata:

There was an error trying to log you in: 'this._settings.loginMode is undefined'

The *.csproj fix worked, but it would be nice not to have to modify an out of the box template that uses all the MS products (Git, B2C, App Service).

<ItemGroup>
    <TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" />
</ItemGroup>
Lemraj commented 2 years ago

Hi @crbrz @mkArtakMSFT, I am having the same issue and the workaround seems not to work for me. After applying the workaround, the wasm app hangs on "Checking login state..." image

Just to be sure that the issue is not related to my solution, i have created a new wasm app, added authentication and got exactly the same issue.

thanks in advance for any help