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

[Bug] Working with CoreWebView2 in MAUI Blazor (hybrid) in combination with Identity results in compile errors

Open edwinvandriel opened this issue 2 years ago • 41 comments

Hi,

I'm building a Maui blazor (hybrid) application. To handle some browser (webview) permissions I'm using a custom DialogPermissionRequestHandler which uses CoreWebView2, CoreWebView2PermissionRequestedEventArgs and some enumerations. All this to make the camera working from blazor hybrid and works fine.

When I'm adding the Microsoft.Identity.Client nuget package version 4.46.0 my application breaks. Here are the lines from the build log:


.\Platforms\Windows\SilentPermissionRequestHandler.cs(8,43,8,55): error CS0433: The type 'CoreWebView2' exists in both 'Microsoft.Web.WebView2.Core, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e' and 'Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b'

.\Platforms\Windows\SilentPermissionRequestHandler.cs(8,64,8,104): error CS0433: The type 'CoreWebView2PermissionRequestedEventArgs' exists in both 'Microsoft.Web.WebView2.Core, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e' and 'Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b'

.\Platforms\Windows\IPermissionRequestHandler.cs(5,36,5,48): error CS0433: The type 'CoreWebView2' exists in both 'Microsoft.Web.WebView2.Core, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e' and 'Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b'

.\Platforms\Windows\IPermissionRequestHandler.cs(5,57,5,97): error CS0433: The type 'CoreWebView2PermissionRequestedEventArgs' exists in both 'Microsoft.Web.WebView2.Core, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e' and 'Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b'

.\Platforms\Windows\DialogPermissionRequestHandler.cs(16,49,16,61): error CS0433: The type 'CoreWebView2' exists in both 'Microsoft.Web.WebView2.Core, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e' and 'Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b'

.\Platforms\Windows\DialogPermissionRequestHandler.cs(16,70,16,110): error CS0433: The type 'CoreWebView2PermissionRequestedEventArgs' exists in both 'Microsoft.Web.WebView2.Core, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e' and 'Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b'

.\Platforms\Windows\DialogPermissionRequestHandler.cs(45,49,45,75): error CS0433: The type 'CoreWebView2PermissionKind' exists in both 'Microsoft.Web.WebView2.Core, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e' and 'Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b'

.\Platforms\Windows\DialogPermissionRequestHandler.cs(10,37,10,63): error CS0433: The type 'CoreWebView2PermissionKind' exists in both 'Microsoft.Web.WebView2.Core, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e' and 'Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b'

.\Platforms\Windows\DialogPermissionRequestHandler.cs(10,65,10,92): error CS0433: The type 'CoreWebView2PermissionState' exists in both 'Microsoft.Web.WebView2.Core, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e' and 'Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b'


When I remove the nuget package everything works again.

Looks like Identity package introduces these WebView2 classes but from other libraries. The custom permission handler I used are from this sample https://github.com/MackinnonBuck/MauiBlazorPermissionsExample

edwinvandriel avatar Aug 10 '22 09:08 edwinvandriel

Yes on net6-windows10... target framework, MSAL references:

image

We are planning to remove this reference, but I do not have an ETA. It should be possible to force your app to use the exact reference.

bgavrilMS avatar Aug 10 '22 10:08 bgavrilMS

Do you or somebody else know how I can force to use the exact reference?

edwinvandriel avatar Aug 10 '22 10:08 edwinvandriel

See https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/dependencies

You can try to reference the version you need directly in your project, at the top level, for the windows target framework (net6-windows10..)

bgavrilMS avatar Aug 10 '22 10:08 bgavrilMS

I just want to leave out the Microsoft.Web.WebView2.Core assemblies from the reference <PackageReference Include="Microsoft.Identity.Client" Version="4.46.0" /> I've tried some things from the link you've mentioned but without any luck. An example on how to do this would be appreciated.

edwinvandriel avatar Aug 10 '22 12:08 edwinvandriel

@SameerK-MSFT was looking to create a MAUI + B2C + Blazor sample and may be able to help.

bgavrilMS avatar Aug 10 '22 13:08 bgavrilMS

I can think of a workaround. Instead of referencing MSAL's net5-windows10 version, you can force reference a different target, i.e. the netcoreapp2.1 target.

Something like

<ItemGroup>

    <PackageReference Include="Microsoft.Identity.Client" ExcludeAssets="all" GeneratePathProperty="true" Version="4.46.0"/>

    <Reference Include="Microsoft.Identity.Client" >

      <HintPath>$(PkgMicrosoft_Identity_Client)\lib\netcoreapp2.1\Microsoft.Identity.Client.dll</HintPath>

    </Reference>

  </ItemGroup>

bgavrilMS avatar Aug 10 '22 13:08 bgavrilMS

related issue: https://github.com/microsoft/microsoft-ui-xaml/issues/5689

aetos382 avatar Nov 16 '22 09:11 aetos382

Microsoft.Identity.Client references Microsoft.Web.WebView2 since v4.28.0. Therefore, libraries that reference this library (such as Microsoft.Graph, Microsoft.Data.SqlClient, and so on) are not compatible with WinUI 3.0.

aetos382 avatar Nov 16 '22 10:11 aetos382

Interesting, thanks for the update @aetos382 ... We do have a WinUI 3.0 sample and we have tested it with MSAL and did not see any error. Can you provide some repro steps?

bgavrilMS avatar Nov 16 '22 10:11 bgavrilMS

https://github.com/aetos382/WinUIApp

This repository contains a simple WinUI 3 app. I just created an empty WinUI 3 app in VS 2022, placed WebView2 into the MainWindow, and added the Microsoft.Data.SqlClient package to the reference. When I build this project, I get the following error.

error CS0433: The type 'CoreWebView2' exists in both 'Microsoft.Web.WebView2.Core, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e' and 'Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b'

If I remove the reference to the Microsoft.Data.SqlClient package from the project, or downgrade its version to v4.x, I will be able to build it. This is because v5.x of the Microsoft.Data.SqlClient package requires the Microsoft.Identity.Client package v4.45 or later, which references the Microsoft.Web.WebView2 package.

aetos382 avatar Nov 16 '22 13:11 aetos382

I have the same issue when using Microsoft.Identity.Client in a WinUIproject.

The problem arises when making a self contained WinUI application. @bgavrilMS did you make a packaged WinUI app? Because then you would not see the problem.

ancker1 avatar Nov 17 '22 09:11 ancker1

Based on the comment from @aetos382 I downgraded "Microsoft.Identity.Client" to version 4.27.1 and "Microsoft.Identity.Client.Extensions.Msal" to version 2.18.0 and it not works.

ancker1 avatar Nov 17 '22 13:11 ancker1

Any news on this? it is absolutely ridiculous that Microsoft.Identity.Client breaks WinUi entirely.

rossirpaulo avatar Nov 23 '22 18:11 rossirpaulo

@rossirpaulo - correct me if I am wrong, but the scenario is broken if you use MSAL and WebView2 in a WebUI application?

The only workaround I can think of is to force target MSAL's net6 dll instead of net6-windows10.0.17763 - like described here - https://duanenewman.net/blog/post/a-better-way-to-override-references-with-packagereference/

CC @pmaytak as you are looking at cleaning up some references and @SameerK-MSFT as on-call enginner

bgavrilMS avatar Nov 23 '22 22:11 bgavrilMS

Or any other package that includes a version of WebView2. So, for instance, any WinUi desktop (native) application currently breaks when using Microsoft.Identity.Client too; That's because the Windows SDK and Identity.Client both have references to WebView2. The problem is that there's no way to exclude a sub-package from a package; and that is exactly what we need to do to make it work in case of the Identity.Client NuGet reference -- as the WebView2 package is referenced therein.

rossirpaulo avatar Nov 23 '22 22:11 rossirpaulo

Did you try to follow this article that explains how to deal with diamond dependencies? https://learn.microsoft.com/en-us/dotnet/standard/library-guidance/dependencies

Is the problem that the 2 versions of WebView2 SDK are incompatible with each other?

bgavrilMS avatar Nov 23 '22 22:11 bgavrilMS

The doubly referenced context means two DLLs are created on compilation; an error is announced on evidence of this duplication.

(versioning is not necessarily the problem; in my case, they have the same version)

rossirpaulo avatar Nov 23 '22 22:11 rossirpaulo

Any news on this!?!

rossirpaulo avatar Dec 14 '22 03:12 rossirpaulo

I did research on this issue. A few points:

  • The error does not occur on MAUI or MAUI Blazor project

  • The provided sample https://github.com/aetos382/WinUIApp is a WinUI project and when it has WebView2 control and Microsoft.Identity.Client package, it produces the compilation error.

  • This compiler error started occurring with version 4.28.0 i.e. it has been occurring since 03/18/2021 i.e. for almost 2 years.

I tried the avenues mentioned in the comments including:

  • Using HintPath
  • Changing the version range

The original bug is in [WindowsAppSDK] (https://github.com/MicrosoftEdge/WebView2Feedback/issues/1921)

Here is the workaround

Hope this helps.

SameerK-MSFT avatar Jan 26 '23 23:01 SameerK-MSFT

Thanks for the research @SameerK-MSFT. @rossirpaulo @aetos382 @ancker1 - can you please try the workaround provided ?

bgavrilMS avatar Jan 27 '23 16:01 bgavrilMS

We're going to keep this open until a solution is found or the bug is officially rejected by Windows team. Please the workaround found by @SameerK-MSFT

https://github.com/microsoft/microsoft-ui-xaml/issues/5689#issuecomment-982128778

bgavrilMS avatar Feb 06 '23 12:02 bgavrilMS

@bgavrilMS : posted work around does not work (I really, really tried).

Here is a simple demo app showing that the work around does not work: https://github.com/baskren/WinUi3_MSAL_SelfContained_WorkAround_Fail

Here is a simple demo app illustrating the original failure: https://github.com/baskren/WinUi3_MSAL_SelfContained_Fail

baskren avatar Feb 09 '23 13:02 baskren

I am not sure there is anything we can do about it. The WinUI team has stated that it's a problem on their end, but I am not sure they will fix it as it's been there for a while :(.

If you can think of a workaround, happy to discuss.

bgavrilMS avatar Feb 09 '23 16:02 bgavrilMS

@bgavrilMS : Thank you for taking the time to respond. Sincerely appreciated.

baskren avatar Feb 09 '23 22:02 baskren

Closing as workaround exists.

bgavrilMS avatar May 24 '23 15:05 bgavrilMS

Workaround exists? Please share!

baskren avatar May 24 '23 15:05 baskren

the workaround does not work. Maui is a joke. So many reference issues and double reference and bullshit. Cant believe they shipped this framework, nothing on it has worked since it was released. It takes four times as long to sort out all the issues than it does actually creating a project. What a joke microsoft.

IQTechSolutions avatar Oct 09 '23 07:10 IQTechSolutions

Reopening issue.

bgavrilMS avatar Oct 09 '23 11:10 bgavrilMS

Before this issue is closed again, would it be possible to post a POC demo project? AFAIK, every claimed work around or fix may have been successful for one person but, for me, it has not. Have a POC project would be helpful in many ways.

baskren avatar Oct 09 '23 12:10 baskren

Before this issue is closed again, would it be possible to post a POC demo project? AFAIK, every claimed work around or fix may have been successful for one person but, for me, it has not. Have a POC project would be helpful in many ways.

I am in the same situation.

ancker1 avatar Oct 09 '23 14:10 ancker1