11

我有一个与 WebAPI 对话的 Angular 应用程序,并且用户通过 Azure Active Directory 进行身份验证

我按照这里的示例https://github.com/Azure-Samples/active-directory-angularjs-singlepageapp-dotnet-webapi并能够针对 AD 对用户进行身份验证并将其传递给 Web API。

但是我想访问 Web API 中的 Graph API 并获取当前用户配置文件信息。我该如何设置?

更新以提供有关设置的更多上下文:

我有一个托管 html 和 javascript 文件的网站 (site.domain1.com),它制作了一个 SPA 应用程序。我在 api.domain2.com 上托管了 Web API。身份验证针对 Azure AD,使用带有 ADAL.js 和 angular-adal 的 OAuth 隐式流。我想在 SPA 中进行身份验证以获取 API 的 accessToken。我希望在 API 中作为查询 Graph API 的请求的一部分,以获取有关当前用户登录的更多信息。

我能够获取 API 的 accessToken 并且它目前生成 Claims Principal。问题是用我在 Web API 中的当前身份查询 Graph API。

更新:

我不想为 Web API 提供管理员权限,但我更愿意将用户同意从浏览器转发到网站和 Web api。

我对位于此处的示例使用了类似的方法https://github.com/Azure-Samples/active-directory-dotnet-webapi-onbehalfof

它适用于我的测试广告并且不适用于生产广告的问题。说用户需要在使用 Graph Api 之前集中应用程序。(对于生产 AD,我只有用户可以添加用户权限但不能添加应用程序权限。我的猜测是该方案工作我需要 AD 的全局管理员首先集中注意力)。最终,我最终将用于网站和 Web API 的 Azure AD 应用程序合并在一起,并且它使用与 Bootstrap Tokens 相同的 On Behalf Of 方法工作。但我想知道如何让它与 2 个应用程序一起正常工作。

4

3 回答 3

0

权限可在 Azure 的应用程序配置页面中配置,您可以选择您希望应用程序访问的内容。

至于确认,您作为管理员可以选择。用户接受与您的应用共享数据或管理员确认租户下的所有人。

这才是正确的方法。我的前端和后端就是这样工作的。

于 2016-04-07T00:14:39.027 回答
0

请参阅示例:https ://github.com/Azure-Samples/active-directory-dotnet-graphapi-web 。示例中有一些代码可以访问 Graph API 并获取用户配置文件:

ClientCredential credential = new ClientCredential(clientId, appKey);
AuthenticationResult result = authContext.AcquireTokenSilent(graphResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

// Call the Graph API manually and retrieve the user's profile.
string requestUrl = String.Format(CultureInfo.InvariantCulture, graphUserUrl, HttpUtility.UrlEncode(tenantId));
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = await client.SendAsync(request);

// Return the user's profile in the view.
if (response.IsSuccessStatusCode) {
    string responseString = await response.Content.ReadAsStringAsync();
    profile = JsonConvert.DeserializeObject<UserProfile>(responseString);
}

您可以从此处查看更多信息:https ://azure.microsoft.com/en-us/documentation/articles/active-directory-code-samples/#calling-azure-ad-graph-api

于 2016-03-08T08:19:15.143 回答
-1

所以我知道这是一个旧的,但我只是遇到了它,因为我有同样的问题/设置。它需要一些挖掘,但我认为这个链接可能会帮助那些有同样问题的人。

https://azure.microsoft.com/en-us/resources/samples/active-directory-dotnet-webapi-onbehalfof/

这同时使用了 WPF 客户端和具有单独 API 的 SPA,并从 Web API 而非 spa 或客户端调用 Graph API。

祝大家好运。

于 2019-04-25T19:45:27.910 回答