【问题标题】:Issues Accessing Microsoft Graph API Data访问 Microsoft Graph API 数据的问题
【发布时间】: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


【解决方案1】:

您正在使用 ADAL 库,该库使用旧的 Azure AD V1 身份验证端点。您应该使用使用 Azure AD V2 身份验证终结点的 MSAL 库。

我建议让您的生活更轻松,并获取 Microsoft.Graph.Auth Nuget 包,然后使用此代码,而不必创建自己的代码 委托认证提供者

IConfidentialClientApplication clientApplication = AuthorizationCodeProvider.CreateClientApplication(clientId, redirectUri, clientCredential);
AuthorizationCodeProvider authenticationProvider = new AuthorizationCodeProvider(clientApplication, scopes); 

【讨论】:

  • 感谢您的回复,设法让 Graph API 工作,购买搜索 MSAL 并尝试一些示例,直到一个工作。在上面的 clientCredential 代码中,还有其他方法可以从当前登录的用户那里获取吗?用最少的编码工作。
猜你喜欢
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-29
  • 2022-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多