[英]Azure AD with Bearer token authentication for Web API not working throwing error as “Authorization has been denied for this request.”
[英]Azure AD authentication token fails web api authorization
我在Azure上有一個需要授權的Web API,並且我正在使用Azure AD對帳戶進行身份驗證並生成訪問令牌。
我可以通過兩種不同的方式通過ADAL成功地使用ADAL從Azure AD獲取訪問令牌,但是只有一個由Web-api授權,而另一個失敗。
以下是對帳戶進行交互身份驗證並且令牌已由Web-api授權
result = AuthenticationContext.AcquireTokenAsync(resource, clientId, redirectUri, new PlatformParameters(PromptBehavior)).Result;
資源是Web API應用程序ID(GUID)。
以下是使用給定的用戶名和密碼以非交互方式對帳戶進行身份驗證,但令牌未得到Web API的授權
UserPasswordCredential cred = new UserPasswordCredential(userName, password);
result = AuthenticationContext.AcquireTokenAsync(resource, clientId, cred).Result;
資源= https:// {tenant} / {api name}。
Web-api調用如下:
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = await httpClient.GetAsync(ApplicationCallUri);
兩種方法都返回相同的AuthenticationResult對象(除了令牌和時間戳),而且我看不到為什么第二個對象授權失敗。
Web-api響應為“對此請求的授權已被拒絕”。
由於身份驗證通過兩種方式均能成功進行,因此我認為它必須與Web API兼容。 非常感謝您的幫助。 謝謝。
感謝juunas指出了Audience參數,我意識到Web api被設置為僅期望兩個受眾值之一的令牌。 我為承載身份驗證添加了第二個選項,它適用於兩種情況。
謝謝朱尼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.