Skip to content
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

[Bug] MSAL Broker unable to load 'msalruntime.dll' when packaged as MSIX #3740

Closed
Tracked by #3375
exchange321 opened this issue Oct 7, 2022 · 12 comments · Fixed by #3954
Closed
Tracked by #3375

[Bug] MSAL Broker unable to load 'msalruntime.dll' when packaged as MSIX #3740

exchange321 opened this issue Oct 7, 2022 · 12 comments · Fixed by #3954

Comments

@exchange321
Copy link

exchange321 commented Oct 7, 2022

Logs and network traces

Microsoft.Identity.Client.MsalClientException: Unable to load DLL 'msalruntime': The specified module could not be found. (Exception from HRESULT: 0x8007007E) See https://aka.ms/msal-net-wam#troubleshooting ---> System.DllNotFoundException: Unable to load DLL 'msalruntime': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at Microsoft.Identity.Client.NativeInterop.API.x64.MSALRUNTIME_Startup()
   at Microsoft.Identity.Client.NativeInterop.API.x64.Startup()
   at Microsoft.Identity.Client.NativeInterop.Module.AddRef(String handleName)
   at Microsoft.Identity.Client.NativeInterop.Core..ctor()
   at Microsoft.Identity.Client.Broker.RuntimeBroker.<>c.<.cctor>b__21_0()
   --- End of inner exception stack trace ---
   at Microsoft.Identity.Client.Broker.RuntimeBroker.<>c.<.cctor>b__21_0()
   at System.Lazy`1.CreateValue()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Lazy`1.get_Value()
   at Microsoft.Identity.Client.Broker.RuntimeBroker.IsBrokerInstalledAndInvokable(AuthorityType authorityType)
   at Microsoft.Identity.Client.ClientApplicationBase.<GetAccountsFromBrokerAsync>d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Identity.Client.ClientApplicationBase.<GetAccountsInternalAsync>d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Identity.Client.ClientApplicationBase.<GetAccountsAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Identity.Client.ClientApplicationBase.<GetAccountsAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Clipchamp.Domain.Services.MicrosoftAuthService.<ClearCache>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Clipchamp.Domain.Services.MicrosoftAuthService.<SignIn>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Clipchamp.Domain.Handlers.SignInWithMicrosoftHandler.<Handle>d__4.MoveNext()
Inner Exception: System.DllNotFoundException: Unable to load DLL 'msalruntime': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at Microsoft.Identity.Client.NativeInterop.API.x64.MSALRUNTIME_Startup()
   at Microsoft.Identity.Client.NativeInterop.API.x64.Startup()
   at Microsoft.Identity.Client.NativeInterop.Module.AddRef(String handleName)
   at Microsoft.Identity.Client.NativeInterop.Core..ctor()
   at Microsoft.Identity.Client.Broker.RuntimeBroker.<>c.<.cctor>b__21_0()

Which version of MSAL.NET are you using?
Microsoft.Identity.Client 4.47.2
Microsoft.Identity.Client.Broker 4.47.2-preview
Microsoft.Identity.Client.NativeInterop 0.13.0

Platform
C# WPF (.NET Framework 4.8) packaged as MSIX through Windows Application Packaging Project

What authentication flow has the issue?
WAM

Is this a new or existing app?
Discovered the issue when migrating from Microsoft.Identity.Client.Desktop to Microsoft.Identity.Client.Broker as per this documentation

Repro

Result : You should now see the Windows Web Account Manager UI showing up and prompting you to sign in to the app. You can use the MS Credentials or use the following user details to sign-in. But sign in is actually not required. The WAM (Web Account Manager) UI is from the Native dll (msalruntime.dll) packaged along with Microsoft.Identity.Client.Broker

  • Now set the WPF.Package as the start up project
  • Start the project "WPF.Package" project.

Error :

Microsoft.Identity.Client.MsalClientException: 'Unable to load DLL 'msalruntime': The specified module could not be found. (Exception from HRESULT: 0x8007007E) See https://aka.ms/msal-net-wam#troubleshooting'

Inner Exception
DllNotFoundException: Unable to load DLL 'msalruntime_x86': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Extended error info and workarounds can be found in the repo link

Expected behavior
Using WAM as a broker should not fail

Actual behavior
MSAL throws Microsoft.Identity.Client.MsalClientException: Unable to load DLL 'msalruntime': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

@bgavrilMS bgavrilMS added this to the 4.48.0 milestone Oct 7, 2022
@bgavrilMS bgavrilMS moved this from Triage to Estimated/Committed in MSAL Customer Trust / QM Oct 7, 2022
@bgavrilMS bgavrilMS moved this from Estimated/Committed to Blocked/Waiting for reply in MSAL Customer Trust / QM Oct 25, 2022
@pmaytak pmaytak removed this from the 4.48.0 milestone Nov 2, 2022
@bgavrilMS bgavrilMS moved this from Blocked/Waiting for reply to Estimated/Committed in MSAL Customer Trust / QM Nov 3, 2022
@azchohfi
Copy link

azchohfi commented Nov 4, 2022

It seems like this happens because the runtime folder is only on the publish folder. I believe there is a way to provide a PublishProfile to wapproj, but I'm not sure.
Btw, this also happens with .NET Core apps.

@azchohfi
Copy link

azchohfi commented Nov 4, 2022

I came up with a workaround. In your wapproj file, add this:

  <ItemGroup>
    <PackageReference Include="Microsoft.Identity.Client.NativeInterop" Version="0.13.0" GeneratePathProperty="true"/>
  </ItemGroup>
  <ItemGroup>
    <ContentWithTargetPath Include="$(PkgMicrosoft_Identity_Client_NativeInterop)\runtimes\win-x86\native\*" TargetPath="runtimes\win-x86\native\%(Filename)%(Extension)" CopyToOutputDirectory="PreserveNewest" />
    <ContentWithTargetPath Include="$(PkgMicrosoft_Identity_Client_NativeInterop)\runtimes\win-x64\native\*" TargetPath="runtimes\win-x64\native\%(Filename)%(Extension)" CopyToOutputDirectory="PreserveNewest" />
    <ContentWithTargetPath Include="$(PkgMicrosoft_Identity_Client_NativeInterop)\runtimes\win-arm64\native\*" TargetPath="runtimes\win-arm64\native\%(Filename)%(Extension)" CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

@pmaytak pmaytak added this to the 4.49.0 milestone Nov 4, 2022
@aries-zhang
Copy link

aries-zhang commented Nov 7, 2022

It looks like although VisualStudio generates the following warning message, the dlls are still successfully copied to the target path.

Severity	Code	Description	Project	File	Line	Suppression State
Warning		The element 'ItemGroup' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003' has invalid child element 'ContentWithTargetPath' in namespace 'http://schemas.microsoft.com/developer/msbuild/2003'. List of possible elements expected: 'Item, Reference, SDKReference, COMReference, COMFileReference, PackageReference, FrameworkReference, DotNetCliToolReference, Xdcmake, Bscmake, ClCompile, ClInclude, Midl, ResourceCompile, PreLinkEvent, CustomBuildStep, Manifest, ProjectConfiguration, ProjectCapability, NativeReference, ProjectReference, Compile, EmbeddedResource, Content, Page, Resource, ApplicationDefinition, None, BaseApplicationManifest, Folder, Import, Service, WebReferences, WebReferenceUrl, FileAssociation, BootstrapperFile, PublishFile, TargetPlatform, Analyzer, AndroidResource, ProguardConfiguration, CodeAnalysisDependentAssemblyPaths, CodeAnalysisDictionary, CodeAnalysisImport, AppxManifestSchema, StoreManifestSchema, AppxHashUri, PRIResource, AppxSystemBinary, AppxReservedFileName, AppxManifestFileNameQuery, AppxManifestImageFileNameQuery, AppxManifest, StoreAssociationFile, AppxManifestMetadata, PlatformVersionDescription, AppxPackagePayload, ComFilesOutputGroupOutputs, CopyWinmdArtifactsOutputGroupOutputs, CopyLocalFilesOutput....	WPF.Package

In a SampleApp I created for reproducing this error, the only issue is the same error still happens when directly hit "run" in VisualStudio. I'm guessing maybe these dlls are not yet ready in the target path when VisualStudio starts the debug session. It won't complain Unable to load DLL 'msalruntime' if the app is started by going into the published location and double clicking it though.

@pmaytak pmaytak modified the milestones: 4.49.0, 4.50.0 Dec 15, 2022
@bgavrilMS bgavrilMS moved this from Estimated/Committed to In Progress in MSAL Customer Trust / QM Jan 11, 2023
@bgavrilMS
Copy link
Member

bgavrilMS commented Jan 18, 2023

ping @gladjohn on repro steps.

@gladjohn
Copy link
Contributor

So far in my investigation the issue seems to be related to MSIX packaging and it's limitation on not being able to check the environment variables. Our runtime dll load logic sets the environment variable

exploring how this can be achieved with MSIX

@bgavrilMS
Copy link
Member

@gladjohn - we can drop the env variable stuff and rely on LoadLibrary. https://github.com/libgit2/libgit2sharp/blob/1af49546917b69499f2d41cf844509c53b37eaca/LibGit2Sharp/Core/NativeMethods.cs#L134

Originally the logic was relying on LoadLibrary but @alextok argued that this will break UWP apps. While correct, we are now all in agreement that UWP apps are not in scope for msal_runtime integration.

@gladjohn
Copy link
Contributor

I should get an update from .NET team by eod today (1/23), if there are other ways to get around this or if there is a viable workaround. I will post my findings here and discuss to the approach we want to take. Will add LoadLibrary as Option 1

@bgavrilMS bgavrilMS removed their assignment Feb 7, 2023
@pmaytak pmaytak linked a pull request Feb 10, 2023 that will close this issue
1 task
@pmaytak pmaytak moved this from In Progress to Fixed in MSAL Customer Trust / QM Feb 10, 2023
@pmaytak pmaytak closed this as completed Feb 10, 2023
@luronumen
Copy link

luronumen commented Oct 7, 2023

Hi Guys,

This issue is also reproducible when a C# WinForm application (.NET 4.8 Framework) is published thru ClickOnce (Visual Studio 2022 - v17.7.4) Should I have to open other issue to track it once this already closed?

The msalruntime.dll library is not included in the ClickOnce publish folder.

Thanks in advance,
Luciano

@bgavrilMS
Copy link
Member

Hi @luronumen - let's please open a new issue and mention this one. There is too much history here that might not be relevant.

@luronumen
Copy link

Hi @bgavrilMS

I have open the #4369 issue to track it. Please let me know if you need any other info.

Thanks in advanced,
Luciano

@ravikumarpatra
Copy link

I am working in blazor maui app, whre we are pulling meeting info form exchange. this is working fine in windows and android, but in iOS (IPAD) it shows the error System.DllNotFoundException: 'msalruntime_arm64'

@bgavrilMS
Copy link
Member

@ravikumarpatra - the broker package is only expected to work on Windows and later on Mac and Linux. We don't have support for MacCatalyst (there is a feature request already). I suggest that for mac-catalyst target framework you don't reference Microsoft.Identity.Client.Broker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

8 participants