调用MS Graph API时错误CompactToken解析失败,错误代码:80049217

时间:2020-08-26 02:42:50

标签: c# asp.net-core microsoft-graph-api microsoft-graph-sdks

我们实现了获取已登录用户MFA中使用的电话号码的功能。我们在拥有服务帐户(具有全局管理员角色)的地方使用密码授予流程,该帐户将代表用户调用MS Graph API。

我们能够获得访问令牌。但是,在调用MS Graph时遇到以下错误。

错误:

ServiceException:代码:InvalidAuthenticationToken 消息:CompactToken解析失败,错误代码:80049217

MS Graph API调用:

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

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

这是我们在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;
}

我已在线搜索有关该错误的信息,但找不到解决方案。 感谢您的回复。谢谢。

0 个答案:

没有答案