【发布时间】:2019-04-30 14:57:16
【问题描述】:
目前在将 Microsoft Graph API 集成到我的 ASP.NET Core 2.2 Web 应用程序 (MVC) 时遇到问题。使用 “工作或学校帐户”:“云 - 单一组织” 使用 两因素 Azure 登录身份验证。
使用 代码示例 1 代码我正在尝试获取图形查询:-
https://graph.microsoft.com/v1.0/me/
从响应头返回姓
我目前遇到的问题是我在代码行收到错误:-
var objMessages = objGraphClient.Me.Request().GetAsync().Result;
带有错误消息:“不存在或其查询的引用属性对象之一不存在”。
// #############
// Code Sample 1
// #############
// Graph Api.
string strResource = "https://graph.microsoft.com";
string SecretId = "<Secret Id>";
// Azure Ad.
Uri strInstance = new Uri("https://login.microsoftonline.com/");
string strDomain = "<Domain>.onmicrosoft.com";
string strTenantId = "<Tenant Id>";
string strClientId = "<Client Id>";
string strCallbackPath = "/signin-oidc";
// The authority to ask for a token: your azure active directory.
string strAuthority = new Uri(strInstance, strTenantId).AbsoluteUri;
AuthenticationContext objAuthenticationContext = new AuthenticationContext(strAuthority);
ClientCredential objClientCredential = new ClientCredential(strClientId, SecretId);
// Acquire Token.
AuthenticationResult objAuthenticationResult = objAuthenticationContext.AcquireTokenAsync(strResource, objClientCredential).Result;
// Get bearer token.
GraphServiceClient objGraphClient = new GraphServiceClient(new DelegateAuthenticationProvider(
async request =>
{
// This is adding a bearer token to the httpclient used in the requests.
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", objAuthenticationResult.AccessToken);
}));
// The next line produces an error :: does not exist or one of its queried reference-property objects are not present.
var objResult = objGraphClient.Me.Request().GetAsync().Result;
Debug.WriteLine($"{objResult.Surname}");
如果我将上面的 代码示例 1 更改为下面的 代码示例 2,并传入成功登录后从 Microsoft Graph Explorer 获得的 tokenPlease() 请求,这将有效,返回姓氏成功,表明他们在我的 Bearer 令牌中可能存在问题:-
// #############
// Code Sample 2
// #############
// Get bearer token.
GraphServiceClient objGraphClient = new GraphServiceClient(new DelegateAuthenticationProvider(
async request =>
{
// This is adding a bearer token to the httpclient used in the requests.
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer","ERf54f2f...Etc");
}));
// The next line now works.
var objResult = objGraphClient.Me.Request().GetAsync().Result;
Debug.WriteLine($"{objResult.Surname}");
对此的任何帮助将不胜感激!
【问题讨论】:
-
会发生什么而不是您的预期?除非我错过了,否则我看不到问题的描述。
-
感谢您的回复,在原始帖子的附加 cmets 中添加。简而言之,当为登录用户使用 Me 别名时,我收到一个错误。
标签: c# asp.net microsoft-graph-api asp.net-core-2.2