Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 3 tasks
YakubMordon opened this issue Aug 29, 2024 · 0 comments
Open
1 of 3 tasks

Comments

@YakubMordon
Copy link

YakubMordon commented Aug 29, 2024

Category

  • Question
  • Documentation issue
  • Bug

Expected or Desired Behavior

Hi, I want to use Microsoft.Identity.Client.Desktop for authorization via embedded WebView2 in WinUI3, my code for authorization is based on example at the bottom of question, because when I try to authorize via default Browser, after authorization it drops such exception:

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

This code example works perfectly for WPF, but it doesn't work at all with WinUI 3

Observed Behavior

When I added library Microsoft.Identity.Client.Desktop to project with Microsoft.WindowsAppSdk and Microsoft.Graph, error was shown in building procedure:

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.

There was also needed update of whole class, because Microsoft.Identity.Client 5.0.0+ doesn't contain for example DelegateAuthenticateProvider.

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.

Steps to Reproduce

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;
    }
}

Thank you for help😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant