Quickstart: Call the Microsoft Graph API MSAL Hangs

Fritz Switzer 281 Reputation points
2022-07-15T01:56:26.003+00:00

I'm using the Tutorial found at the following location:

Tutorial: Call the Microsoft Graph API from a Universal Windows Platform (UWP) application

Using the program as written, I'm getting a frequent error where the Connect To Service/ Login box hangs after entering the password. I have changed the ClientID and used multiple Nuget lib versions for both the Microsoft.Graph and Microsoft.Identity.Client.

Like I said, this error condition is inconsistent and does not happen all the time, but a majority of times.

I've used multiple machines and the results are the same. And I have been strugling with this problem for some time. I am not getting an "error" because the app just "hangs".

Here is some additional information.

The first snippet is from a WinForms app...It works as expected. 1. Signin 2. password 3. Shows a success connection and displays the Access Token (Here is that code with the ClientId hidden)

using System.Drawing;  
using System.Linq;  
using System.Reflection;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
using static System.Windows.Forms.VisualStyles.VisualStyleElement;  
  
namespace WindowsFormsApp1  
{  
    public partial class Form1 : Form  
    {  
        private static string ClientId = "xxxx....xxxxx";  
  
        private AuthenticationResult authResult = null;  
        private string[] scopes = { "Notes.ReadWrite" };  
  
        // The MSAL Public client app  
        private static IPublicClientApplication PublicClientApp;  
  
        private static PublicClientApplicationBuilder app;  
  
        private static string MSGraphURL = "https://graph.microsoft.com/v1.0/";  
  
        private IEnumerable<IAccount> accounts = null;  
        private IAccount firstAccount = null;  
  
        public Form1()  
        {  
            InitializeComponent();  
  
            SignInBtn_Click(null, null);  
        }  
  
        private async void SignInBtn_Click(object sender, EventArgs e)  
        {  
            var app = PublicClientApplicationBuilder.Create(ClientId).Build();  
  
            var result = await app.AcquireTokenInteractive(scopes).ExecuteAsync();  
            authResult = result;  
  
            accounts = await app.GetAccountsAsync();  
            firstAccount = accounts.FirstOrDefault();  
  
            string userName = firstAccount.Username.ToString();  
            string homeaccountId = firstAccount.HomeAccountId.ToString();  
            string objectId = firstAccount.HomeAccountId.ObjectId.ToString();  
  
            MessageBox.Show(result.AccessToken);  
        }  

The second snippet that is pretty much identical to the first snippet from a UWP app that will work sometimes. But most of the time it hangs after the password is entered. Just a flashing "blue dot" in the upper left hand corner.

using Microsoft.Identity.Client;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Linq;  
using System.Runtime.CompilerServices;  
  
using Windows.UI.Xaml.Controls;  
  
namespace SOAPV2.Views  
{  
    public sealed partial class MainPage : Page, INotifyPropertyChanged  
    {  
        private static string ClientId = "2c7ea4af-a717-4141-a147-77eb4bd96869";  
  
        private AuthenticationResult authResult = null;  
        private string[] scopes = { "Notes.ReadWrite" };  
  
        // The MSAL Public client app  
        private static IPublicClientApplication PublicClientApp;  
  
        private static PublicClientApplicationBuilder app;  
  
        private static string MSGraphURL = "https://graph.microsoft.com/v1.0/";  
  
        private IEnumerable<IAccount> accounts = null;  
        private IAccount firstAccount = null;  
  
        public MainPage()  
        {  
            InitializeComponent();  
  
            SignIn();  
        }  
  
        private async void SignIn()  
        {  
            var app = PublicClientApplicationBuilder.Create(ClientId).Build();  
  
            var result = await app.AcquireTokenInteractive(scopes).ExecuteAsync();  
  
            accounts = await app.GetAccountsAsync();  
            firstAccount = accounts.FirstOrDefault();  
  
            //MessageBox.Show(result.AccessToken);  
        }  

I am using the latest Microsoft.Identity.client from NuGet

I have struggled to find a solution to this problem. Looking forward to your answer.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,113 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,904 questions
{count} votes

Accepted answer
  1. 2022-07-17T19:57:55.953+00:00

    Hello @Fritz Switzer , your code works fine. Since you're having trouble with just one deployment the next step is to debug the application on the remote machine where the problem arises. Additionally, you might troubleshoot your app using the App Center free tier.

    Let us know if you need additional assistance. If the answer was helpful, please accept it and complete the quality survey so that others can find a solution.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.