AzureAD / microsoft-authentication-library-for-dotnet

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

[Bug] "cannot convert from 'Microsoft.Identity.Client.BrokerOptions' to 'bool'" error when update Microsoft.Identity.Client.Broker to version 4.61.0 #4759

Closed luronumen closed 1 month ago

luronumen commented 1 month ago

Library version used

4.61.0

.NET version

.NET 8.0 (WinForm App)

Scenario

PublicClient - desktop app

Is this a new or an existing app?

The app is in production, and I have upgraded to a new version of MSAL

Issue description and reproduction steps

1- Update Microsoft.Identity.Client.Broker from 4.60.3 to 4.61.0 via NuGet: "cannot convert from 'Microsoft.Identity.Client.BrokerOptions' to 'bool'" error in the following code line:

var authenticationResult = await PublicClientApplicationBuilder.Create("<MicrosoftClientId>").WithTenantId("common").WithDefaultRedirectUri().WithBroker(new BrokerOptions(BrokerOptions.OperatingSystems.Windows)).Build().AcquireTokenInteractive(new string[] { "user.read" }).WithParentActivityOrWindow(form).ExecuteAsync();

Relevant code snippets

var authenticationResult = await PublicClientApplicationBuilder.Create("<MicrosoftClientId>").WithTenantId("common").WithDefaultRedirectUri().WithBroker(new BrokerOptions(BrokerOptions.OperatingSystems.Windows)).Build().AcquireTokenInteractive(new string[] { "user.read" }).WithParentActivityOrWindow(form).ExecuteAsync();

Expected behavior

No issue should occur when upgrading from version 4.60.3 to version 4.61.0

Identity provider

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

Regression

4.60.3

Solution and workarounds

N/A

bgavrilMS commented 1 month ago

Hi @luronumen - if you are targeting net7-windows or net8-windows, then you have to reference Microsoft.Identity.Client.Broker to get the broker logic, which now fully lives in that package.

luronumen commented 1 month ago

Hi @bgavrilMS

I am targeting to net8.0-windows and after adding using Microsoft.Identity.Client.Broker; the issue is NOT reproducible.

Thank you very much for the workaround!

luronumen commented 1 month ago

Hi @bgavrilMS

The compilation issue is NOT reproducible with the mentioned workaround, but the following runtime error is now happening:

Type: Microsoft.Identity.Client.MsalClientException Message: A window handle must be configured. See https://aka.ms/msal-net-wam#parent-window-handles Stack Trace: at Microsoft.Identity.Client.Platforms.Features.RuntimeBroker.RuntimeBroker.AcquireTokenInteractiveAsync(AuthenticationRequestParameters authenticationRequestParameters, AcquireTokenInteractiveParameters acquireTokenInteractiveParameters) at Microsoft.Identity.Client.Internal.Broker.BrokerInteractiveRequestComponent.FetchTokensAsync(CancellationToken cancellationToken) at Microsoft.Identity.Client.Internal.Requests.InteractiveRequest.FetchTokensFromBrokerAsync(String brokerInstallUrl, CancellationToken cancellationToken) at Microsoft.Identity.Client.Internal.Requests.InteractiveRequest.GetTokenResponseAsync(CancellationToken cancellationToken) at Microsoft.Identity.Client.Internal.Requests.InteractiveRequest.ExecuteAsync(CancellationToken cancellationToken) at Microsoft.Identity.Client.Internal.Requests.RequestBase.<>c__DisplayClass11_1.<b__1>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.Identity.Client.Utils.StopwatchService.MeasureCodeBlockAsync(Func`1 codeBlock) at Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync(CancellationToken cancellationToken) at Microsoft.Identity.Client.ApiConfig.Executors.PublicClientExecutor.ExecuteAsync(AcquireTokenCommonParameters commonParameters, AcquireTokenInteractiveParameters interactiveParameters, CancellationToken cancellationToken)

Do you know how to fix it?

bgavrilMS commented 1 month ago

Hi @luronumen - with 4.60.3 did you not have to configure a window handle? Was under the impression it is needed.

WAM requires a window handle so that it can parent itself to it. The api to do that is WithParentActivityOrWindow on the PublicClientApplication builder object. See https://learn.microsoft.com/en-us/entra/msal/dotnet/acquiring-tokens/desktop-mobile/wam#parent-window-handles

For WinForms, ever Form has a Handle property associated with it (this.Handle). See https://learn.microsoft.com/en-us/windows/apps/develop/ui-input/retrieve-hwnd#winforms-with-c

luronumen commented 1 month ago

Hi @bgavrilMS

With 4.60.3 I used WithParentActivityOrWindow(form) and it worked fine. Now, in the 4.61.0 version, based in your previous comment, replacing the WithParentActivityOrWindow(form) by WithParentActivityOrWindow(form.Handle) is working fine.

Once again, thank you very much!

bgavrilMS commented 1 month ago

I am not sure why this happened, but I am glad passing the windows.Handle works.