dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.

Home Page:https://asp.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.NET 8 preview7 Blazor WASM AAD authentication ends with There was an error trying to log you in: '"undefined" is not valid JSON'

markushaslinger opened this issue · comments

commented

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I am getting the same issue as described in #44981 again: after coming back from login at Azure AD the user sees 'There was an error trying to log you in: '"undefined" is not valid JSON'' at the 'authentication/login-failed' route.

However, after manually navigating around the token is available so it kind-of worked anyway.

Same as with the previous issue, adding the following solves the issue, so I suspect a similar cause:

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

Also: error does not appear during development, only after publish also pointing to the linker

Expected Behavior

No error should be displayed after the redirect if the operation was successful.

Steps To Reproduce

Standard Blazor WASM application using Microsoft.Authentication.WebAssembly.Msal (8.0.0-preview.7.23375.9), set up to use 'redirect' and not the 'pop up' auth option.

Exceptions (if any)

No response

.NET Version

8.0.100-preview.7.23376.3

Anything else?

No response

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. 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.

I'm getting a similar behavior ("undefined" is not valid JSON') but in this case I'm using Azure ADB2C with an IdentityServer4 external provider.
I tried using

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

but the issue is still there. Any suggestions would be appreciated.

@halter73 isn't this something you've addressed already (your PR to the some AAD library I think) ?

In the 8.0.0 release:

  • This issue appears to be a regression from #44981 (cc @javiercn).
  • It results in a "LogInFailed" UI without any visible notifications.
  • No errors are logged in the browser console or in the RemoteAuthenticatorView.LogInFailed context (notably, downgrading MSAL to 7.0.13 yields an error: "undefined" is not valid JSON).
  • Disabling trimming for Microsoft.Authentication.WebAssembly.Msal when publishing the app resolves the issue immediately.

I believe this should not be classified as "technical debt" for future ".NET 9 Planning", but rather recognized as a regression that could potentially cause significant issues for users.

I experienced the same issue when upgrading to .NET 8.0 with Azure B2C. I had to add the following to get it to work.

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

Specifying just Microsoft.Authentication.WebAssembly.Msal alone did not work for me.

@kyleherzog Sorry for the confusion. Actually I do have both the MSAL and WebAssembly.Authentication rooted and it is probably the Microsoft.AspNetCore.Components.WebAssembly.Authentication which is the important one for this symptom.

I can confirm. The issue is solved in our case by mentioning only one root assembly:

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

i have same problem,but i don't know how to fix it. nomatter i add

but it doesn't work

Using Net8.0, TrimmerRootAssembly does not fix the issue.
Please help.

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. 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.

Something that caught me out was I also needed to clear cache after adding

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

Edit: This actually didn't fix it.

@martinblampied could you please check if you need both assemblies listed ? For me, it worked nicely listing only Microsoft.AspNetCore.Components.WebAssembly.Authentication

@czlatea Doesn't work for me; tried both and just Authentication in both Client and Server projects using .net 8

Same issue on .NET 8 and I have tried adding both to the trimmer exception. Pushing this to .NET 9 is unacceptable. This has been going on long enough. It's been years now.

Had the same issue. Going from .NET 7 -> .NET 8, I had to add the following in the Client csproj:

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

This resolved the issue.

Another thing I noticed was that it was only in Microsoft Edge the problem occured, in Google Chrome there was never an issue.

In .NET 8 and have the same issue. The TrimmerRootAssembly fix did not work for me. The problem occurs in both Chrome and Edge.

Worth noting though that it doesn't happen locally, only in release mode in a deployed environment. And it happens on both log in and log out.

I also tried the workarounds from issue #43293, but no success with those either.

I have tried with both adding TrimmerRootAssembly and disabling trimming altogether with PublishTrimmed = false and the problem persists.

This is showstopping bug happening in a critical piece of an LTS release. Postponing the fix to .NET 9 seems absurd to me, given that these workarounds don't work for everybody.

In my case with:

  • NET 8.0.100
  • Microsoft.Authentication.WebAssembly.Msal v8.0.0,
  • Blazor WASM (not hosted)
    didn't work even configure:
<ItemGroup>
	<TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" />
	<TrimmerRootAssembly Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" />
</ItemGroup>

in the project file.
I commented this line to make it work in release (but the app lost the redirect functionality):
//options.ProviderOptions.LoginMode = "redirect";

Update: In several tests, the error raises some times, but the login works (in some cases must refresh with F5), the logout works from AADB2C but in the blazor page got the error. The logout-callback returns 404.

To all who report that the trimming setting does not address their issue – it's probable you're encountering a different problem. A key symptom of this issue is: it only appears after publishing your project (which is when assembly trimming occurs). If you're facing authentication problems in a development environment (like when running from Visual Studio), you're dealing with a separate issue, and the TrimmerRootAssembly setting will not assist you.

To all who report that the trimming setting does not address their issue – it's probable you're encountering a different problem. A key symptom of this issue is: it only appears after publishing your project (which is when assembly trimming occurs). If you're facing authentication problems in a development environment (like when running from Visual Studio), you're dealing with a separate issue, and the TrimmerRootAssembly setting will not assist you.

The fix below does not help me, and I only experience the issue when deploying (Azure App Service). Had no issue with .NET 7.

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

To all who report that the trimming setting does not address their issue – it's probable you're encountering a different problem. A key symptom of this issue is: it only appears after publishing your project (which is when assembly trimming occurs). If you're facing authentication problems in a development environment (like when running from Visual Studio), you're dealing with a separate issue, and the TrimmerRootAssembly setting will not assist you.

The fix below does not help me, and I only experience the issue when deploying (Azure App Service). Had no issue with .NET 7.

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

same for me. The fix does not help me, and it only happens in the deployed app, since the update to .NET 8

You could attempt to turn off trimming by setting <PublishTrimmed>false</PublishTrimmed> to verify if trimming is the root cause of the issue. If this resolves the problem, you can then proceed to "root" the assemblies one-by-one to identify which one is effective.

yes with <PublishTrimmed>false</PublishTrimmed> the problem is gone. Even it was a hard way to test it. I had to clean all obj folders manually. Before that mono was out of sync and a cleanup did not work.

so I tried

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

with trimming again and delete all obj folders. And it is working.

yes with <PublishTrimmed>false</PublishTrimmed> the problem is gone. Even it was a hard way to test it. I had to clean all obj folders manually. Before that mono was out of sync and a cleanup did not work.

so I tried

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

with trimming again and delete all obj folders. And it is working.

I did not try <PublishTrimmed>false</PublishTrimmed>, but I did make sure this time to clean all relevant bin+obj folders - now deploying works for me with only one assembly "rooted":

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

Had the same issue. Going from .NET 7 -> .NET 8, I had to add the following in the Client csproj:

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

This resolved the issue.

Another thing I noticed was that it was only in Microsoft Edge the problem occured, in Google Chrome there was never an issue.

Thanks! you were right A) it was only in Edge B) it fixed my issue

Quick update on my experience: We have the Msal nuget package as a transitive dependency, buried fairly deep in some class libraries. Adding the TrimmerRootAssembly for WebAssembly.Authentication and/or PublishTrimmed = false to that library wasn't sufficient.

It did work however once we added TrimmerRootAssembly to that library and every library that referenced it, along with the actual Blazor WASM project.

This might be a little overkill, but it'll do the trick for now.

<PublishTrimmed>false</PublishTrimmed> worked for us,

With below settings

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

When changing the csproj file (with one of the previous entries), don't forget to delete the bin and obj folders...

I was still getting this issue after trying the TrimmerRootAssembly fix and the PublishTrimmed false fixes, but my build pipeline was running an incremental build so the previously trimmed assemblies were still getting used.
Try disabling incremental build or cleaning your build environments if you are also getting this issue despite trying the other fixes.

I've opted to disable trimming entirely as I was additionally getting an issue with Telerik components that <PublishTrimmed>false</PublishTrimmed> also fixed. I don't know how many other issues this could be causing in my dependencies so I don't trust having assembly trimming enabled.

(I also tested reverting the changes and it did bring back both of my issues)

Are there any updates on this? I am also facing this issue with my .NET 8 application deployed to an Azure AppService using Azure ADB2C.

I have a hosted Blazor WASM application. In which project(s) do I put the <PublishTrimmed>false</PublishTrimmed> to see if that works for me?

Are there any updates on this? I am also facing this issue with my .NET 8 application deployed to an Azure AppService using Azure ADB2C.

I have a hosted Blazor WASM application. In which project(s) do I put the <PublishTrimmed>false</PublishTrimmed> to see if that works for me?

you have to put it in the wasm project and please don't forget to delete the bin and obj folders for all projects before you deploy

Just setup a new project on the hosted wasm model and I had to put this in the client project for the authentication to work for azure AD. On the Microsoft Identity Platform it worked without this.

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

please don't forget to delete the bin and obj folders for all projects before you deploy

This doesn’t really apply in a CI/CD setup does it? I run dotnet publish in my build, publish the artifacts from the publish folder, and then my release deploys those artifacts to the Azure AppService.

I am facing this issue on a IIS hosted Blazor WASM application. The only way I could make it work has been to disable <PublishTrimmed>false</PublishTrimmed> on PropertyGroup of the .csproj file.

Thankful I found this thread. Experienced the issue upgrading our .NET 7.0 Blazor WASM PWA to .NET 8.0. Applied the suggested changes, which alongside removing the bin/obj folders and a full rebuild worked a treat.
<ItemGroup> <TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" /> <TrimmerRootAssembly Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication"/> </ItemGroup>

why is this still an issue in .NET 8? yes there is this fix but come on

I'm having trouble reproducing the There was an error trying to log you in: '"undefined" is not valid JSON' error even after publishing https://github.com/halter73/BlazorHostedWasmAAD with the following commands:

> git clean -xdf
Removing .vs/
Removing Client/bin/
Removing Client/obj/
Removing Server/bin/
Removing Server/obj/
Removing Shared/bin/
Removing Shared/obj/
> cd Server
> dotnet publish -c Release
MSBuild version 17.8.3+195e7f5a3 for .NET
  Determining projects to restore...
  Restored H:\temp\WasmAAD\Shared\WasmAAD.Shared.csproj (in 132 ms).
  Restored H:\temp\WasmAAD\Client\WasmAAD.Client.csproj (in 228 ms).
  Restored H:\temp\WasmAAD\Server\WasmAAD.Server.csproj (in 229 ms).
  WasmAAD.Shared -> H:\temp\WasmAAD\Shared\bin\Release\net8.0\WasmAAD.Shared.dll
  WasmAAD.Client -> H:\temp\WasmAAD\Client\bin\Release\net8.0\WasmAAD.Client.dll
  WasmAAD.Client (Blazor output) -> H:\temp\WasmAAD\Client\bin\Release\net8.0\wwwroot
  WasmAAD.Server -> H:\temp\WasmAAD\Server\bin\Release\net8.0\WasmAAD.Server.dll
  Optimizing assemblies for size may change the behavior of the app. Be sure to test after publishing. See: https:
  //aka.ms/dotnet-illink
  Optimizing assemblies for size. This process might take a while.
  WasmAAD.Server -> H:\temp\WasmAAD\Server\bin\Release\net8.0\publish\
> cd .\bin\Release\net8.0\publish\
> dotnet .\WasmAAD.Server.dll --urls https://localhost:7189
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7189
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: H:\temp\WasmAAD\Server\bin\Release\net8.0\publish
info: Microsoft.Hosting.Lifetime[0]
      Application is shutting down... 

I then navigate to https://localhost:7189 with a private browsing window and log in successfully without any errors in either the server logs or client developer console.

Home page while logged in

To create the BlazorHostedWasmAAD project, I did the following:

You can see each step in the projects commits. I did not have to use TrimmerRootAssembly or PublishTrimmed in my csproj.

I see that there are a lot of people have run into this issue, so I'm sure I'm missing something. Can anyone figure out what I may doing wrong?

Hi @markushaslinger. 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.

@halter73 I forked your repo and set it up like my project, relevant changes:

  • Redirect Mode is set: markushaslinger/BlazorHostedWasmAAD_repro@786e08e
  • It's not hosted by a .NET process but I just throw it up on a Nginx which hosts it
  • Instead of options.ProviderOptions.DefaultAccessTokenScopes.Add("https://{TENANT}.onmicrosoft.com/{SERVER API APP CLIENT ID}/API.Access"); I use options.ProviderOptions.DefaultAccessTokenScopes.Add("{SERVER API APP CLIENT ID}/.default"); (I honestly can't remember why any more)

After these changes (+ entering my own tenant and client of course) and running dotnet publish -c Release (afaik the Release tag is now the default anyway, but I'm so used to writing it) I threw the wwwroot content on the server, tried the login, and:

image

Don't think my setup is totally unusal, but please tell me if I'm doing something I'm not supposed to do (the redirect is a requirement though, can't change that). I hope that helps you to reproduce the issue!

I see that there are a lot of people have run into this issue, so I'm sure I'm missing something. Can anyone figure out what I may doing wrong?

Hi @halter73. Many of us in this thread only experienced the issue when we deployed to Azure, localhost worked fine.

FWIW (to help reproduce) - I had this issue after following the steps here https://learn.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/standalone-with-microsoft-entra-id

Solution runs fine on my localhost but shows the JS error once deployed to Azure.

The same workaround works for me.

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

Is this expected to be folded in to the 8.0.4 release?

For anyone following along, I had a similar issue here #54515 that I was hoping would be fixed with 8.0.4. I updated and that does not seem to be the case.

Can we stop closing tickets until something is confirmed as fixed AND released? I even asked in my other issue how I'd confirm that this works locally so I don't have to do a full build and deploy, but no dice.

pls fix

Hoping this gets fixed soon. Loading app state from an encoded URL (like in #54514 ) has been broken for months in our app and is a pretty basic/critical feature to be broken for so long.

Key functionality (authentication) of a framework (in this case Blazor) being shipped completely broken out of the box in an LTS release when you've known about it since preview builds is completely unacceptable, closing an issue when it is NOT fixed, and expecting users to jump through hoops to find a CLOSED GitHub issue with a workaround is even worse. Doing things like that, alongside other shortcomings of auth in Blazor (like using a deprecated version of oidc-client-js) basically ensures Blazor will never see serious adoption.

It looks like the fix might be included in the upcoming 8.0.5, which based on the release cadence so far, I'd guestimate that it'll be out on May 7th

#54655

You're right though, they really need to do something about the underlying oidc-client, there hasn't been a release of it since 2021 and the repo was archived in March 2022, the replacement oidc-client-ts is actively maintained, regularly updated and well supported - they really do need to do something.

@milkyjoe90 afaik patches come out the second Tuesday of each month (but are not guaranteed), so that would be May 14th. Takes a bit for things to propagate (like packages getting posted to NuGet and what not), so I'd just check the 15th.

@milkyjoe90 afaik patches come out the second Tuesday of each month (but are not guaranteed), so that would be May 14th. Takes a bit for things to propagate (like packages getting posted to NuGet and what not), so I'd just check the 15th.

Ahhh Patch Tuesday, of course! Can’t believe I missed that! Lines up perfectly with all the other MS patches that get released!