Azure Active Directory:web api调用另一个web api

问题描述 投票:0回答:1

我有两个不同的Web API应用程序(web-api-app1web-api-app2)与Azure Active Directory身份验证集成。

我可以使用一些控制台应用程序单独调用这两个API应用程序,如下所示:

AuthenticationContext ac = new AuthenticationContext("https://login.windows.net/[AD Tenent]");

var clientCredentials = new ClientCredential("app id", "app key");
AuthenticationResult ar = ac.AcquireTokenAsync("https://web-api-app1", clientCredentials).Result;

string result = string.Empty;

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ar.AccessToken);

HttpResponseMessage response = httpClient.GetAsync("https://web-api-app1/odata").Result;

if (response.IsSuccessStatusCode)
{
    result = response.Content.ReadAsStringAsync().Result;
}

Console.WriteLine(result);

现在我的要求是我需要从web-api-app2调用web-api-app1(web API调用web API调用)。我怎么做?谢谢!

c# asp.net-web-api azure-active-directory
1个回答
2
投票

你需要实现on-behalf-ofdocs for v1 endpoints)流程。

app1应使用在用户身份验证时获取的自己的访问令牌向app2请求访问令牌。

app1的清单应包括访问app2的许可。

如果应用程序位于不同的租户中,app2应首先由app1租户管理员同意。

The official .net example app

© www.soinside.com 2019 - 2024. All rights reserved.