microsoft-authentication-library-for-dotnet
microsoft-authentication-library-for-dotnet copied to clipboard
[Bug] Working with CoreWebView2 in MAUI Blazor (hybrid) in combination with Identity results in compile errors
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
Yes on net6-windows10...
target framework, MSAL references:
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.
Do you or somebody else know how I can force to use the exact reference?
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..)
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.
@SameerK-MSFT was looking to create a MAUI + B2C + Blazor sample and may be able to help.
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>
related issue: https://github.com/microsoft/microsoft-ui-xaml/issues/5689
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.
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?
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.
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.
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.
Any news on this? it is absolutely ridiculous that Microsoft.Identity.Client breaks WinUi entirely.
@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
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.
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?
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)
Any news on this!?!
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.
Thanks for the research @SameerK-MSFT. @rossirpaulo @aetos382 @ancker1 - can you please try the workaround provided ?
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 : 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
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 : Thank you for taking the time to respond. Sincerely appreciated.
Closing as workaround exists.
Workaround exists? Please share!
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.
Reopening issue.
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.
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.