【发布时间】:2018-06-27 07:51:30
【问题描述】:
我获得了一个访问令牌,可以使用以下代码获取图形客户端:
string graphResourceID = "https://graph.windows.net";
string tenantID = ConfigurationManager.AppSettings["ida:Domain"];
string aadInstance = "https://login.microsoftonline.com/" + tenantID +
"/oauth2/v2.0/token";
Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential credential = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientId, appKey);
AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance);
authenticationContext.TokenCache.Clear();
var authResult = await authenticationContext.AcquireTokenAsync(graphResourceID,clientcred);
然后尝试使用令牌通过AD graph api获取登录用户的信息:
Uri servicePointUri = new Uri(graphResourceID);
Uri serviceRoot = new Uri(servicePointUri, tenantID);
ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot,async () => await GetTokenForApplication());
var result = await activeDirectoryClient.Users.Where(u => u.ObjectId.Equals(userObjectID)).ExecuteAsync();
IUser user = result.CurrentPage.ToList().First();
return View(user);
但是,这会返回以下错误:
"error": {
"code": "Authorization_IdentityNotFound",
"message": "The identity of the calling application could not be established.",
"innerError": {
"request-id": "2bdae8ff-d935-4e01-80a1-78cbc8acf4de",
"date": "2017-08-09T18:07:40"
我确保 mu B2C 应用程序具有 Windows Active Directory 的“读写目录数据”权限:
有人可以帮忙吗?被困在这个问题上一段时间了。 TIA
编辑
我也尝试过使用 Microsoft.Graph,但最终得到了同样的错误。另外,对于 B2C 用户,我认为现在最好还是坚持使用 Azure Ad Graph api:https://dev.office.com/blogs/microsoft-graph-or-azure-ad-graph
【问题讨论】: