Jacob Mordon
Jacob Mordon

Reputation: 13

Collision between Microsoft.Identity.Client.Desktop and Microsoft.WindowsAppSdk in WinUI 3

I would like to know, how to solve error like this:

error NETSDK1152: Found multiple publish output files with the same relative path:

MyProject\obj\x64\Debug\net7.0-windows10.0.19041.0\win10-x64\MsixContent\Microsoft.Web.WebView2.Core.dll

microsoft.web.webview2\1.0.864.35\lib\netcoreapp3.0\Microsoft.Web.WebView2.Core.dll.

This error appeared when I downloaded Microsoft.Identity.Client.Desktop, which also contains WebView2. Microsoft.Identity.Client.Desktop was needed to authorize through embedded webview, because PublicClientApplicationBuilder isn't working correctly for me. Exception says:

Original exception: AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.

To reproduce this bug, you need to do this steps:

  1. Download Microsoft.Identity.Client.Desktop, Microsoft.Graph and MicrosoftWindowsAppSdk.
  2. Copy this code:
public class AuthenticationHelper
{
    static string clientId = App.Current.Resources["ida:ClientID"].ToString();
    public static string[] Scopes = { "Files.Read" };

    private static readonly IPublicClientApplication IdentityClientApp;

    public static string TokenForUser = null;
    public static DateTimeOffset Expiration;

    private static GraphServiceClient graphClient = null;

    static AuthenticationHelper()
    {
        IdentityClientApp = PublicClientApplicationBuilder.Create(clientId)
            .WithDefaultRedirectUri()
            .WithWindowsEmbeddedBrowserSupport()
            .Build();
    }

    public static GraphServiceClient GetAuthenticatedClient()
    {
        if (graphClient == null)
        {
            // Create Microsoft Graph client.
            try
            {
                graphClient = new GraphServiceClient(
                    "https://graph.microsoft.com/v1.0",
                    new DelegateAuthenticationProvider(
                        async (requestMessage) =>
                        {
                            var token = await GetTokenForUserAsync();
                            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
                            requestMessage.Headers.Add("SampleID", "uwp-csharp-photobrowser-sample");

                        }));
                return graphClient;
            }

            catch (Exception ex)
            {
                Debug.WriteLine("Could not create a graph client: " + ex.Message);
            }
        }

        return graphClient;
    }

    public static async Task<string> GetTokenForUserAsync()
    {
        AuthenticationResult authResult;

        var accounts = await IdentityClientApp.GetAccountsAsync();
        var firstAccount = accounts.FirstOrDefault();

        try
        {
            authResult = await IdentityClientApp.AcquireTokenSilent(Scopes, firstAccount).ExecuteAsync();
            TokenForUser = authResult.AccessToken;
        }

        catch (Exception ex)
        {
            //this.logger.Error(ex);
            if (TokenForUser == null || Expiration <= DateTimeOffset.UtcNow.AddMinutes(5))
            {
                authResult = await IdentityClientApp.AcquireTokenInteractive(Scopes)
                    .WithAccount(firstAccount)
                    .WithPrompt(Microsoft.Identity.Client.Prompt.SelectAccount)
                    .WithUseEmbeddedWebView(true)
                    .ExecuteAsync();

                TokenForUser = authResult.AccessToken;
                Expiration = authResult.ExpiresOn;
            }
        }

        return TokenForUser;
    }
}

But same code as the one I attached above is perfectly working for me on WPF, but not working in WinUI, because Microsoft.Identity.Client 5.0+ isn't supporting some classes from UWP example.

I tried to add extern aliases to WindowsAppSdk and Microsoft.Identity.Client.Desktop, so when app is compiled it should be recognized as other WebView2, but it hadn't helped. I also tried to add ignoring of error with duplicates using ErrorOnDuplicatePublishOutputFiles, but it hadn't helped also

Upvotes: 0

Views: 106

Answers (0)

Related Questions