4

We implement to get the phone numbers being used in MFA of the signed-in user. We use password grant flow where we have a service account(with Global admin role) that will call MS Graph API on behalf of the user.

We are able to get the access token. However, when making a call to MS Graph encounters the error below.

Error:

ServiceException: Code: InvalidAuthenticationToken Message: CompactToken parsing failed with error code: 80049217

MS Graph API call:

    MicrosoftGraphClientSDK client = new MicrosoftGraphClientSDK();            
    var graphClient = client.GetAuthenticatedClient();

// Error encountered here:
    var phones = await graphClient.Me.Authentication.PhoneMethods[{objectiD of the user}].Request().GetAsync();

This is how we get the access token in GetAuthenticatedClient

public MicrosoftGraphClientSDK()
{
    _app_public = PublicClientApplicationBuilder.Create(clientID)
          .WithAuthority("https://login.microsoftonline.com/{tenantID}")
          .Build();
}

public Beta.GraphServiceClient GetAuthenticatedClient()
{
    var accessToken = GetUserAccessTokenAsync();

    var delegateAuthProvider = new DelegateAuthenticationProvider((requestMessage) =>
    {
        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.ToString());
        return Task.FromResult(0);
    });

    _graphClient = new Beta.GraphServiceClient(delegateAuthProvider);

    return _graphClient;
}

public async Task<string> GetUserAccessTokenAsync()
{
    AuthenticationResult result;

    var accounts = await _app_public.GetAccountsAsync();
    if (accounts.Any())
    {
        result = await _app_public.AcquireTokenSilent(_scopes, accounts.FirstOrDefault())
                          .ExecuteAsync();
    }
    else
    {
        SecureString password = new SecureString();

        foreach (char c in pass)
            password.AppendChar(c);

        result = await _app_public
                                .AcquireTokenByUsernamePassword(_scopes, username, password)
                                .ExecuteAsync();
    }

    return result.AccessToken;
}

I have search online about the error but could not get figure out the solution. I appreciate your response. Thanks.

Abolfazl
  • 1,592
  • 11
  • 32

0 Answers0