我正在开发守护程序应用程序。我已经下载了示例应用程序以尝试查看它是否可以运行,但是也从该应用程序中获得了相同的错误。我的管理员已尽其所能检查一切,但没有发现任何问题。理想的最终结果是拥有一个可以代表用户发送电子邮件并从某些邮箱中读取邮件的程序。我还需要能够快速确认我的应用程序配置正确。
该程序不会与用户互动,将代表公司运行。
我做了什么:
使用的大量软件包:
我的沙盒代码:
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
关于下一步或调整内容的任何想法,将不胜感激。
提前感谢您的帮助。
我猜问题是您尚未指定目标租户。
您已经这样定义它:
AadAuthorityAudience = AadAuthorityAudience.AzureAdMultipleOrgs
您需要改为指定Azure公共云+租户guid。我现在正在打电话,所以我无法查找确切的语法:/