aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

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

Open markushaslinger opened this issue 1 year ago • 40 comments

Is there an existing issue for this?

  • [X] 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

markushaslinger avatar Aug 09 '23 15:08 markushaslinger

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.

ghost avatar Oct 04 '23 17:10 ghost

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.

hectorm-bmg avatar Oct 20 '23 14:10 hectorm-bmg

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

mkArtakMSFT avatar Nov 08 '23 02:11 mkArtakMSFT

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.

hakenr avatar Nov 15 '23 16:11 hakenr

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 avatar Nov 15 '23 21:11 kyleherzog

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

hakenr avatar Nov 16 '23 00:11 hakenr

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>

czlatea avatar Nov 21 '23 08:11 czlatea

i have same problem,but i don't know how to fix it. nomatter i add <TrimmerRootAssembly Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" /> but it doesn't work

YingXiaoMing avatar Dec 07 '23 03:12 YingXiaoMing

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

jumulr avatar Dec 12 '23 09:12 jumulr

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.

ghost avatar Dec 13 '23 18:12 ghost

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 avatar Dec 15 '23 10:12 martinblampied

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

czlatea avatar Dec 15 '23 12:12 czlatea

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

martinblampied avatar Dec 15 '23 12:12 martinblampied

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.

SeanLeitzinger avatar Dec 17 '23 17:12 SeanLeitzinger

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.

Olsenss avatar Dec 20 '23 09:12 Olsenss

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.

MaddMugsy avatar Dec 20 '23 18:12 MaddMugsy

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.

meronz avatar Dec 31 '23 16:12 meronz

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.

acmesoft-arg avatar Jan 05 '24 06:01 acmesoft-arg

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.

hakenr avatar Jan 05 '24 11:01 hakenr

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>

SimonMouridsen avatar Jan 09 '24 08:01 SimonMouridsen

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

KptNorad avatar Jan 09 '24 09:01 KptNorad

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.

hakenr avatar Jan 09 '24 12:01 hakenr

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.

KptNorad avatar Jan 10 '24 06:01 KptNorad

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>

SimonMouridsen avatar Jan 10 '24 07:01 SimonMouridsen

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

syazdian avatar Jan 13 '24 02:01 syazdian

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.

MaddMugsy avatar Jan 13 '24 04:01 MaddMugsy

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

With below settings

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

Rajesh-Goel avatar Jan 13 '24 05:01 Rajesh-Goel

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

luisabreu avatar Jan 16 '24 16:01 luisabreu

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)

rpcontinuousdelivery avatar Jan 16 '24 17:01 rpcontinuousdelivery

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?

mwasson74 avatar Jan 30 '24 21:01 mwasson74