我想通过它的客户端凭证直接访问一个API,而不是通过任何web应用程序访问。
private async Task<string> GetAutheticationToken(string APITypeSelected, string APIKeySelected=null)
{
string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
string tenant = ConfigurationManager.AppSettings["ida:AADTenant"];
string appKey = ConfigurationManager.AppSettings[APIKeySelected];
string apiID = ConfigurationManager.AppSettings[APITypeSelected];
//appKey = HttpUtility.UrlEncode(appKey);
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
using (HttpClient client = new HttpClient())
{
Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authContext = null;
ClientCredential clientCredential = null;
authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority);
//encodeURIComponent(client_secret);
clientCredential = new ClientCredential(apiID, appKey);
AuthenticationResult authResult = null;
authResult = await authContext.AcquireTokenAsync(apiID, clientCredential);
return authResult.AccessToken;
}
}在执行过程中,我在这一行中得到了下面的错误(AADSTS501051)
authResult = await authContext.AcquireTokenAsync(apiID, clientCredential);AADSTS501051:应用程序“{API}”(DEV)没有分配给应用程序“{API}”(DEV)的角色。
我必须给自己API权限吗。
我需要做的事。
谢谢,
发布于 2020-04-10 03:12:36
首先,如果需要应用程序分配,您需要为应用程序设置一个用户角色。如果没有,就没有问题。如果需要应用程序分配,请返回api权限,并在我的api中为创建的角色授予权限,请参阅Microsoft文档url。
发布于 2019-08-20 17:45:23
那么您想要API本身的访问令牌吗?不确定这是否可能..。
如果这在另一个应用程序,它应该注册为另一个应用程序在Azure。然后,它可以要求API上的应用程序权限,并通过客户端凭据调用它。您可以在这里看到如何定义权限:https://joonasw.net/view/defining-permissions-and-roles-in-aad
如果这是在同一个应用程序中,它会为自己获得一个令牌听起来很奇怪。
发布于 2021-07-29 09:53:22
此错误消息表示您需要在应用程序注册中添加一个"App角色“。您可以首先在{API }上添加一个新的App角色来做到这一点。

然后分配应用程序{API }这个角色(不要忘记给管理员同意)

本质上,这里发生的事情是,您的应用程序注册{API }在{API }上得到了一个角色,用于为听众{API }创建访问令牌,所以:它本身。
https://stackoverflow.com/questions/57572070
复制相似问题