AzureAD / microsoft-authentication-library-for-dotnet

Microsoft Authentication Library (MSAL) for .NET
https://aka.ms/msal-net
MIT License
1.4k stars 341 forks source link

[Bug] MSAL.Net RuntimeBroker doesn't support NativeAOT on windows. #4424

Closed azchohfi closed 11 months ago

azchohfi commented 1 year ago

Library version used

4.57.0

.NET version

8.0

Scenario

PublicClient - desktop app

Is this a new or an existing app?

None

Issue description and reproduction steps

Calling await _app.GetAccountsAsync() with nativeaot turned on throws an exception.

Relevant code snippets

Unhandled exception: MSAL.NetCore.4.57.0.0.MsalClientException:
        ErrorCode: wam_runtime_init_failed
Microsoft.Identity.Client.MsalClientException: A type initializer threw an exception.
 ---> System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
   at System.Reflection.Runtime.Assemblies.RuntimeAssemblyInfo.get_CodeBase()
   at Microsoft.Identity.Client.NativeInterop.Platform.GetExecutingAssemblyDirectory()
   at Microsoft.Identity.Client.NativeInterop.API.GetPackagePathAndSetEnvironmentVariables()
   at Microsoft.Identity.Client.NativeInterop.API..cctor()
   at Microsoft.Identity.Client.NativeInterop.Module.AddRef(String)
   at Microsoft.Identity.Client.NativeInterop.Core..ctor()
   at Microsoft.Identity.Client.Platforms.Features.RuntimeBroker.RuntimeBroker.<>c.<.cctor>b__24_0()
   at Microsoft.Identity.Client.Platforms.Features.RuntimeBroker.RuntimeBroker.<>c.<.cctor>b__24_0()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper, Boolean)
   at System.Lazy`1.CreateValue()
   at Microsoft.Identity.Client.Platforms.Features.RuntimeBroker.RuntimeBroker.IsBrokerInstalledAndInvokable(AuthorityType)
   at Microsoft.Identity.Client.ClientApplicationBase.<GetAccountsFromBrokerAsync>d__21.MoveNext()
   at Microsoft.Identity.Client.ClientApplicationBase.<GetAccountsInternalAsync>d__20.MoveNext()
   at Microsoft.Identity.Client.ClientApplicationBase.<GetAccountsAsync>d__13.MoveNext()
   at Microsoft.Identity.Client.ClientApplicationBase.<GetAccountsAsync>d__12.MoveNext()

Expected behavior

Works with NativeAOT.

Identity provider

Microsoft Entra ID (Work and School accounts and Personal Microsoft accounts)

Regression

No response

Solution and workarounds

No response

azchohfi commented 1 year ago

Related issue: https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/4424

bgavrilMS commented 1 year ago

This bug will have been fixed in the broker interop https://github.com/AzureAD/microsoft-authentication-library-for-cpp/pull/3807/files (private repo) but not yet released.

@ashok672, @iulico-1 - regular release goes out every Thu of every month, so next one is scheduled on Dec 7. Happy to help with an out of band release if needed by partners.

pmaytak commented 11 months ago

Released in 4.58.0.

azchohfi commented 11 months ago

I'm still having issues even with 4.58.0:

Unhandled exception: System.IO.FileNotFoundException: Could not find file 'System.Windows.Forms'.
File name: 'System.Windows.Forms'
   at Internal.Runtime.TypeLoaderExceptionHelper.CreateFileNotFoundException(ExceptionStringID, String) + 0x43
   at Internal.Runtime.CompilerHelpers.ThrowHelpers.ThrowFileNotFoundException(ExceptionStringID, String) + 0x9

Maybe it is related to this? https://github.com/dotnet/winforms/issues/4649

azchohfi commented 11 months ago

I did have an ILC warning during my compilation, mentioning exactly that method: 2>ILC: Method '[msstore]MSStore.CLI.Services.TokenManager.MSALTokenManager+<InitAppAsync>d__16.MoveNext()' will always throw because: Failed to load assembly 'System.Windows.Forms'

azchohfi commented 11 months ago

Oh, actually that was a misconfiguration in my compilation. I had SelfContained=false. It had to be true.