亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

AADSTS501051:應用程序“{API GUID}”(DEV-API) 未分配給應用程序

AADSTS501051:應用程序“{API GUID}”(DEV-API) 未分配給應用程序

C#
白衣染霜花 2023-09-09 17:24:50
我想通過其客戶端憑證直接訪問一個 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 權限?我需要做什么。
查看完整描述

4 回答

?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

如果需要應用程序分配,首先您需要為應用程序創建用戶角色。如果沒有也沒有問題。如果需要應用程序分配,請返回 api 權限并在我的 api 中為創建的角色授予權限.


查看完整回答
反對 回復 2023-09-09
?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

此錯誤消息表明您需要將“應用程序角色”添加到您的應用程序注冊中。您可以通過首先在{API GUID}上添加新的應用程序角色來執行此操作

https://img1.sycdn.imooc.com//64fc3a1b0001770306450357.jpg

然后為應用程序{API GUID}分配此角色(不要忘記給予管理員同意)

https://img1.sycdn.imooc.com//64fc3a2a0001198925550815.jpg

本質上,這里發生的事情是您的應用程序注冊{API GUID}在{API GUID}上獲得了為受眾{API GUID}創建訪問令牌的角色,因此:它本身。



查看完整回答
反對 回復 2023-09-09
?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

啊,所以您想要 API 本身的訪問令牌?不確定這是否可能..

如果這是在另一個應用程序中,則應將其注冊為 Azure AD 中的另一個應用程序。然后,它可以要求 API 上的應用程序權限并通過客戶端憑據調用它。

如果這是在同一個應用程序中,那么它會為自己獲取令牌,這聽起來很奇怪。


查看完整回答
反對 回復 2023-09-09
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

當您使用“authContext.AcquireTokenAsync(apiID, clientCredential);”時?要獲取訪問令牌,您需要使用廣告應用程序的identifierUri作為資源。

https://img4.sycdn.imooc.com/64fc3a430001176109620376.jpg

例如:


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);



查看完整回答
反對 回復 2023-09-09
  • 4 回答
  • 0 關注
  • 184 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號