【问题标题】:AADSTS501051: Application '{API GUID}'(DEV-API) is not assigned to a role for the application '{API GUID}'(DEV-API)AADSTS501051:应用程序“{API GUID}”(DEV-API)未分配给应用程序“{API GUID}”(DEV-API)的角色
【发布时间】:2019-08-20 10:55:31
【问题描述】:

我想通过其客户端凭据直接访问一个 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 GUID}”(DEV-API) 未分配给 应用程序“{API GUID}”(DEV-API) 的角色。

我是否必须向自身授予 API 权限。

我需要做什么。

谢谢,

【问题讨论】:

    标签: c# azure-active-directory azure-api-apps


    【解决方案1】:

    如果需要分配应用程序,首先您需要为应用程序创建一个用户角色。如果不是没有问题。如果需要分配应用程序,请返回 api 权限并在我的 api 中为创建的角色授予权限,请参阅 Microsoft 文档 url

    https://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-protected-web-api-app-registration

    【讨论】:

      【解决方案2】:

      啊,所以您想要 API 本身的访问令牌?不确定这是否可能..

      如果这在另一个应用程序中,它应该在 Azure AD 中注册为另一个应用程序。 然后,它可以要求对 API 的应用程序权限并通过客户端凭据调用它。 您可以在此处查看如何定义权限:https://joonasw.net/view/defining-permissions-and-roles-in-aad

      如果这是在同一个应用程序中,它会为自己获取一个令牌听起来很奇怪。

      【讨论】:

      • 请告诉我如何使用 API。我已经在应用程序中进行了身份验证。我想直接使用 API,而不是通过任何其他应用程序。这就是要求。
      • 不能直接调用API调用的服务层吗?
      • 我需要使用 API ..这是期望。我需要在 Manifest 中更改吗?
      【解决方案3】:

      此错误消息表明您需要在应用注册中添加“应用角色”。您可以通过首先在 {API GUID}

      上添加新的应用角色来实现

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

      基本上这里发生的事情是您的应用注册{API GUID}{API GUID} 上获得了一个角色来为观众创建访问令牌{ API GUID},所以:它自己。

      【讨论】:

        【解决方案4】:

        当您使用“authContext.AcquireTokenAsync(apiID, clientCredential);”时要获取访问令牌,您需要使用广告应用程序的 identifierUri 作为资源。

        例如:

        string tenantId = "your tenant id or name, for example: hanxia.onmicrosoft.com";
                    string clientId = "your client id"; 
                    string resource = "the identifierUri of your ad application ";
                    string clientSecret = "";
        
                ClientCredentia clientCredentia = new ClientCredentia(clientId,clientSecret);
        
                        var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId);
                        AuthenticationResult result = context.AcquireTokenAsync(resource, clientCredentia);
        

        更多详情请参考document

        【讨论】:

        • AADSTS501051:应用程序“{API GUID}”(DEV-API) 未分配给应用程序“{API GUID}”(DEV-API) 的角色。
        • 我认为它需要在 azure API 中进行一些配置更改。
        • 是不是同一个应用保护了api?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-19
        • 1970-01-01
        • 2016-05-22
        • 2022-01-17
        • 1970-01-01
        • 2015-07-17
        • 1970-01-01
        相关资源
        最近更新 更多