microsoft-authentication-library-for-dotnet icon indicating copy to clipboard operation
microsoft-authentication-library-for-dotnet copied to clipboard

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

Open luronumen opened this issue 9 months ago • 3 comments

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

luronumen avatar May 13 '24 17:05 luronumen

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.

bgavrilMS avatar May 14 '24 14:05 bgavrilMS

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 avatar May 14 '24 15:05 luronumen

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.<<RunAsync>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?

luronumen avatar May 15 '24 02:05 luronumen

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

bgavrilMS avatar May 15 '24 14:05 bgavrilMS

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!

luronumen avatar May 15 '24 15:05 luronumen

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

bgavrilMS avatar May 17 '24 15:05 bgavrilMS