【发布时间】:2020-05-15 21:43:03
【问题描述】:
我正在开发一个守护程序应用程序。我已经下载了示例应用程序以尝试查看它是否可以工作,但该应用程序也会出现相同的错误。我的管理员已经尽可能多地检查了他,没有透露任何信息。期望的最终结果是拥有一个可以代表用户发送电子邮件并从特定邮箱读取邮件的程序。我还需要能够快速确认我的应用程序配置正确。
此程序不会与用户交互,而是代表公司运行。
我做了什么:
- 在 aad.portal.azure.com 上创建了应用注册。
- 创建了一个客户端密码。
- 管理员已同意 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(应用程序) )
- Integration Assistant(预览版)显示所有绿色,但 1 项除外,即“使用证书凭据而不是密码凭据(客户端机密)。”。我可以接受这个特别的。
- 在身份验证页面上,这不被视为公共客户端。
使用的 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
任何关于下一步检查或调整的想法将不胜感激。
提前感谢您的帮助。
【问题讨论】:
-
您是否按照此帖子的建议征得管理员同意? stackoverflow.com/questions/49522572/…
标签: c# .net-core azure-active-directory microsoft-graph-api daemon