Authorization_IdentityNotFound从守护程序应用程序访问Microsoft Graph

问题描述 投票:0回答:1

我正在开发守护程序应用程序。我已经下载了示例应用程序以尝试查看它是否可以运行,但是也从该应用程序中获得了相同的错误。我的管理员已尽其所能检查一切,但没有发现任何问题。理想的最终结果是拥有一个可以代表用户发送电子邮件并从某些邮箱中读取邮件的程序。我还需要能够快速确认我的应用程序配置正确。

该程序不会与用户互动,将代表公司运行。

我做了什么:

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

使用的大量软件包:

  • 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 daemon
1个回答
0
投票

我猜问题是您尚未指定目标租户。

您已经这样定义它:

AadAuthorityAudience = AadAuthorityAudience.AzureAdMultipleOrgs

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

© www.soinside.com 2019 - 2024. All rights reserved.