2

On multiple machines, I am having no problem, however, on one machine (Win 10, x64) I'm getting the following error:

Null user was passed in AcquiretokenSilent API.
Pass in a user object or call acquireToken authenticate.

The code is the basic MSAL code and I'm using the MSGraph API. The program "hangs" on the following line:

authResult = await App.PublicClientApp.AcquireTokenAsync(scopes);

Here is the complete method:

private async void SignInButton_Click(object sender, RoutedEventArgs e)
{
  // StatusMsg.Text = string.Empty;

  try
  {
    authResult = await App.PublicClientApp.AcquireTokenSilentAsync(scopes, App.PublicClientApp.Users.FirstOrDefault());
    StatusMsg.Text = "Success Logging In";
  }
  catch (MsalUiRequiredException ex)
  {
    // A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token
    System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");

    try
    {
      authResult = await App.PublicClientApp.AcquireTokenAsync(scopes);
      StatusMsg.Text = "Success Logging In with Scopes";
    }
    catch (MsalException msalex)
    {
      StatusMsg.Text = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}";
    }
  }
  catch (Exception ex)
  {
    StatusMsg.Text = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}";
    return;
  }

  if (authResult != null)
  {
    StatusMsg.Text = "Signed In  ";
    StatusMsg.Text += $"Name: {authResult.User.Name}" + Environment.NewLine;
    StatusMsg.Text += $"Username: {authResult.User.DisplayableId}" + Environment.NewLine;
    StatusMsg.Text += $"Token Expires: {authResult.ExpiresOn.ToLocalTime()}" + Environment.NewLine;
    //StatusMsg.Text += "NotebookName/ID = " + Globals.CurentNotebookName.ToString() + " / " + Globals.CurrentNotebookId.ToString() + Environment.NewLine;
  }
}

Here is my autho and scopes:

private AuthenticationResult authResult = null;
private string[] scopes = { "Notes.ReadWrite" };
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
Trey Balut
  • 1,355
  • 3
  • 19
  • 39
  • The code seems to be following standard guidelines for acquiring tokens using MSAL. I am wondering if this is related to a deadlock occurring due to the entire call stack deviating in any way from [Async/Await - Best Practices in Asynchronous Programming](https://msdn.microsoft.com/en-us/magazine/jj991977.aspx?f=255&MSPPError=-2147217396) – Kalyan Krishna Apr 25 '18 at 23:31

0 Answers0