-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Blazor WASM MSAL authentication not working when app is trimmed #43293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for contacting us. |
Thanks, @javiercn! In the meantime, I was investigating, and found what you already know, that the |
@yugabe, Thank you so much for submitting this issue and the workarounds. This is incredibly helpful. I spent a lot of time trying to figure it out and I was stuck until I saw your comments. |
This seems to still be present in RC2 (publish without disabling trimming entirely, and the problem shows up for me). |
@AlbertoPa I think we had some build issues and this ended up not making it to the RC2 build, but into the RTM build |
@javiercn just FYI, I've been chasing a very similar bug to this all weekend, again with WASM MSAL authentication, but slightly different. Initially I saw the same error as the original poster, but after upgrading to RTM, that error disappeared. However, another error arose, and it ended up also being related to trimming, and the issue is still present in 7.0.0-rtm.22512.1. I dug as much as I could, and here's what seems to be happening: When the app is trimmed, on the initial LoginCallback after a successful login, AuthenticationService.ts never gets a valid AuthenticationContext from the LoginComplete delegate for some reason (I'm guessing a trimmed-out DTO). So, when it tries to stringify the JSON state into browser storage, it's just stringifying "undefined" into a string "undefined" and storing that. The whole process breaks after that, because it's all relying on that state to be there and in JSON format. Then the "undefined" is not valid JSON This also only occurs on the initial login flow -- on page refresh, the login completes successfully. I finally solved this issue by removing
So something is still wonky with trimming and the underlying DTOs that the Interop works with, and this time it's with WebAssembly.Authentication or one of its dependencies. Apologies that I haven't been able to figure out exactly what the difference is yet, but hopefully this helps. |
@josephatkuvio, @javiercn, possibly related: #39839, #44845 It might help pinpointing these issues that at least one of the JSInterop APIs is incorrectly annotated (or rather, it's not). I haven't had a chance to see if this still occurs or not, will bring word in a day or two with the final release. |
Thank you @javiercn I have just tested with RTM. While now trimming does not lead to an exception (no message in the console at all), it still seems to change the authentication behavior with AD B2C. The message I got is now
which does not happen if |
Is there an existing issue for this?
Describe the bug
The MSAL library interop layer for Blazor seems to be subject to it being trimmed of crucial configuration information from the assembly upon it being published. It seems to be a somewhat known problem (#38082) but the issue hasn't been fixed since .NET 6 previews (this happens to me on .NET 7 Preview 7 too), and the docs make no mention of a workaround either. This issue is related, but not a duplicate.
The problem can be observed by configuring a Blazor WASM application (might be hosted) to use Azure AD B2C.
The workaround seems to be (I cannot validate it works for all cases, as I have no idea what scenarios are related) to explicitly set the authentication configuration values from the configuration, AS WELL AS providing the following default scopes, like so:
The symptoms can include in a published app:
common
instance or a different Azure AD instance.interaction already in progress
" message returned from MSAL.null
as being unable to be resolved in the tenant:AADSTS700016: Application with identifier 'null' was not found in the directory '{Azure AD tenant name}'.
I could only reproduce the behavior when not trimming the app in debug mode by explicitly setting the
options.ProviderOptions.Authentication.Authority
to usehttps://login.microsoftonline.com/common/
. NOT setting this property won't reproduce the problems either, as the MSAL initialization will throw an exception not being able to construct the URL.Another possible "workaround" seems to be to disable trimming entirely (not the greatest tradeoff) by setting the
<PublishTrimmed>false</PublishTrimmed>
MSBuild flag on the Client project (or probably via command line flags) or including the MSAL library in the list of assemblies not to trim (<TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" />
).I think the problem is during JSON serialization of the options containing the configuration, it is possibly not properly annotated for being trimmed (it is not annotated at all).
More info for those affected or stumped by this like I was for over a week:
I was following the documentation to create a hosted Blazor app with Azure AD B2C MSAL authentication.
The only other modification was that I set up the default login mode to use "redirect" instead of "popup", because the issues were not straightforward to debug at all.
Everything works fine, but only locally. When publishing the app to Azure App Service (on Linux), the following phenomenon was a string of events wholly unexpected:
{tenant}.b2clogin.com
IdP instance, the user can see the defaulthttps://login.microsoftonline.com/common/
instance instead. Further, the application Client ID is not detected:https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=null&...
Authentication
object.Expected Behavior
I expect following the tutorial should work when the app is deployed, and authentication should be available as configured.
Steps To Reproduce
In the resulting app the configuration won't be picked up by the client MSAL.js correctly.
Exceptions (if any)
No response
.NET Version
No response
Anything else?
No response
The text was updated successfully, but these errors were encountered: