![](/img/trans.png)
[英]Blazor WASM protected with Azure Active Directory can't call protected WebAPI
[英]Calling WebService protected by Azure Active Directory
我已将 web 应用程序部署到 Azure 应用程序服务并设置应用程序服务身份验证以使用 Active Directory。
在控制台应用程序中,我有以下代码。
string baseUrl = "https://testapp.azurewebsites.net/Admin/GetCustomers";
string sAuthority = "https://login.microsoftonline.com/{tennant ID}“;
string sClient = “{“Client ID};
string sClientSecret = “{“ClientSecret};
IConfidentialClientApplication app;
app = ConfidentialClientApplicationBuilder.Create(sClient)
.WithClientSecret(sClientSecret)
.WithAuthority(new Uri(sAuthority))
.Build();
string[] scopes = new string[] { "https://testapp.azurewebsites.net/Admin/.default" };
AuthenticationResult result = null;
result = app.AcquireTokenForClient(scopes).ExecuteAsync().Result;
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = httpClient.GetAsync(baseUrl).Result;
if (response.IsSuccessStatusCode)
{
string json = response.Content.ReadAsStringAsync().Result;
JObject resultData = JsonConvert.DeserializeObject(json) as JObject;
Console.WriteLine("Finished Loading Data!");
}
else
{
Console.WriteLine("Error Happened");
}
根据您的指南,我在 ActiveDirectory 中注册了 2 个应用程序
客户端应用程序有权访问此 scope 作为管理员我授予它并创建了一个 ClinetSecret
我忘了做什么? 史蒂夫
这不起作用,因为您正在获取图形 api scope 的令牌:
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
result = app.AcquireTokenForClient(scopes).ExecuteAsync().Result;
如果您改为调用图形 api,则从该调用返回的令牌将起作用:
string baseUrl = "https://graph.microsoft.com/v1/me"
但这不是你所追求的。 您需要做的是创建一个 scope,它提供对您的 API 的访问权限,以便 AAD 可以发出适当的令牌。
为此,您需要在托管 API 的 web 应用程序时,在 Azure 门户中“公开 API”。 您可以使用您创建的自定义名称 scope 或[app id uri]/.default
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.