【问题标题】:Authorization_IdentityNotFound accessing Microsoft Graph from daemon applicationAuthorization_IdentityNotFound 从守护程序应用程序访问 Microsoft Graph
【发布时间】:2020-05-15 21:43:03
【问题描述】:

我正在开发一个守护程序应用程序。我已经下载了示例应用程序以尝试查看它是否可以工作,但该应用程序也会出现相同的错误。我的管理员已经尽可能多地检查了他,没有透露任何信息。期望的最终结果是拥有一个可以代表用户发送电子邮件并从特定邮箱读取邮件的程序。我还需要能够快速确认我的应用程序配置正确。

此程序不会与用户交互,而是代表公司运行。

我做了什么:

  1. 在 aad.portal.azure.com 上创建了应用注册。
  2. 创建了一个客户端密码。
  3. 管理员已同意 11 个 Microsoft Graph 权限:Mail.Read(应用程序)、Mail.ReadBasic(应用程序)、Mail.ReadBasic.All(应用程序)、Mail.ReadWrite(应用程序)、Mail.Send(应用程序)、 User.Export.All(应用程序)、User.Invite.All(应用程序)、User.ManageIdentities.All(应用程序)、User.Read(委托)、User.Read.All(应用程序)、User.ReadWrite.All(应用程序) )
  4. Integration Assistant(预览版)显示所有绿色,但 1 项除外,即“使用证书凭据而不是密码凭据(客户端机密)。”。我可以接受这个特别的。
  5. 在身份验证页面上,这不被视为公共客户端。

使用的 Nuget 包:

  • Microsoft.Identity.Client 4.13.0
  • Microsoft.Graph 3.5.0

我的沙盒代码:

async void Main()
{
    var graphFacade = new MsGraphFacade();

    Console.WriteLine(await graphFacade.ValidateCredentialsAsync());
}

class MsGraphFacade 
{

    private static async Task<GraphServiceClient> GetGraphApiClient()
    {
        var clientId = "(Redacted)";
        var secret = "(Redacted)";

        var app = ConfidentialClientApplicationBuilder
        .CreateWithApplicationOptions(new ConfidentialClientApplicationOptions{
        ClientId = clientId,
        ClientSecret = secret,
        AadAuthorityAudience = AadAuthorityAudience.AzureAdMultipleOrgs,
        })
        .Build();

        Console.WriteLine("Getting token");
        var token = await app
        .AcquireTokenForClient(new[] { "https://graph.microsoft.com/.default" })
        .ExecuteAsync();

        Console.WriteLine("Got token");
        var accessToken = token.AccessToken;

        var graphServiceClient = new GraphServiceClient(
            new DelegateAuthenticationProvider((requestMessage) =>
        {
            requestMessage
                .Headers
                .Authorization = new AuthenticationHeaderValue("bearer", accessToken);

            return Task.CompletedTask;
        }));

            Console.WriteLine("New client returned.");

            return graphServiceClient;
    }

    public async Task<bool> ValidateCredentialsAsync()
    {
        try
        {
            Console.WriteLine("Attempting something simple");

            var client = await GetGraphApiClient();

            var user = await client.Users
                .Request()
                .Top(1)
                .Select(x => x.DisplayName)
                .GetAsync();

            if (user != null)
            {
                return true;
            }
            return false;
        }
        catch (Exception e)
        {
            Console.WriteLine("2");
            Console.WriteLine(e);
            return false;
        }
    }
} 

代码输出:

Attempting something simple 
Getting token 
Got token 
New client returned. 
2

Code: Authorization_IdentityNotFound Message: The identity of the calling application could not be established. 
    Inner error:
        AdditionalData:
            request-id: f31bc340-1cdf-485f-b852-f1e2822201ef
            date: 2020-05-15T20:24:38
False

任何关于下一步检查或调整的想法将不胜感激。

提前感谢您的帮助。

【问题讨论】:

标签: c# .net-core azure-active-directory microsoft-graph-api daemon


【解决方案1】:

我猜问题是你没有指定目标租户。

你已经这样定义它了:

AadAuthorityAudience = AadAuthorityAudience.AzureAdMultipleOrgs

您需要改为指定 Azure 公共云 + 租户 guid。我现在正在打电话,所以我无法查找确切的语法:/

【讨论】:

  • 对于那些可能来这里寻找答案的人,'TenantId = "(redacted)"' 是选项中使用的内容。 TenantId 和 AadAuthorityAudience 互斥,一次只能指定一个。
猜你喜欢
  • 2020-12-25
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
相关资源
最近更新 更多