久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 AAD 對 Azure API 應(yīng)用程序進行身份驗證時出現(xiàn)

401 error when authenticating to an Azure API App using AAD(使用 AAD 對 Azure API 應(yīng)用程序進行身份驗證時出現(xiàn) 401 錯誤)
本文介紹了使用 AAD 對 Azure API 應(yīng)用程序進行身份驗證時出現(xiàn) 401 錯誤的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個 API 應(yīng)用程序在網(wǎng)關(guān)主機上運行良好,現(xiàn)在網(wǎng)關(guān)主機已被棄用,我正在嘗試遵循 遷移指南.我已經(jīng)使用 2.8.1 SDK 重新部署了我的服務(wù),并且可以使用 AAD 或 Microsoft 帳戶使用瀏覽器登錄該服務(wù),并使用 Swagger 測試該服務(wù).但是,我試圖讓客戶端使用 ClientId 和 Secret 訪問服務(wù).該代碼能夠從 AAD 獲取訪問令牌,但每當我嘗試訪問其中一項服務(wù)資源時,總是會收到 401 錯誤.

I have an API app that has been working fine with a Gateway Host and now that the gateway host is being deprecated I'm trying to follow the Migration Guide. I've redeployed my service using the 2.8.1 SDK and can log into the service with a browser using AAD or a Microsoft account and use Swagger to test the service. However, I'm trying to get a client to access the service using a ClientId and Secret. The code is able to get the access token from AAD but I always get a 401 error whenever I try to access one of the service resources.

當我調(diào)試服務(wù)時,我在日志中看到以下內(nèi)容:

When I debug the service I see the following in the log:

Microsoft.Azure.AppService.Authentication Verbose: 0 : Received request: GET https://[myService].azurewebsites.net/api/[myResource]
Microsoft.Azure.AppService.Authentication Warning: 0 : JWT validation failed: IDX10214: Audience validation failed. Audiences: 'https://[myService].azurewebsites.net/'. Did not match:  validationParameters.ValidAudience: '[AAD ClientId]' or validationParameters.ValidAudiences: 'http://[myService].azurewebsites.net'.
Microsoft.Azure.AppService.Authentication Information: 0 : Sending response: 401.71 Unauthorized
The thread 0x3b00 has exited with code 0 (0x0).

問題似乎在于,請求的受眾是 https,但 validParameters.ValidAudiences 集合僅包含 http.

What appears to be the issue is that the Audience presented with the request is https but the validParameters.ValidAudiences collection only contains http.

我看不到任何配置受眾的方式,并且似乎在 Visual Studio 2015 創(chuàng)建應(yīng)用服務(wù)時正在設(shè)置基于 http 的受眾.有沒有手動編輯 ValidAudience 集合的方法?

I can't see any way of configuring the Audience and it appears that the http based audience is being set when Visual Studio 2015 creates the App Service. Is there a way of manually editing the ValidAudience collection?

作為參考,我的客戶端代碼是:

For reference my client code is:

    private static void Main(string[] args)
    {
        string app_id_url = "https://[myService].azurewebsites.net/";
        string authority = "https://login.windows.net/[myDirectory].onmicrosoft.com/";
        string clientId = "[AAD ClientId]";
        string clientSecret = "[AAD Client Secret]";
        string apiBaseUrl = "https://[myService].azurewebsites.net/";

        string aadToken = GetTokenForApplication(authority, clientId, clientSecret, app_id_url);

        var apiClient = new HttpClient { BaseAddress = new Uri(apiBaseUrl) };
        apiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", aadToken);
        var apiResponse = apiClient.GetAsync(apiBaseUrl + @"api/[myResource]").Result;
        string apiResponseContent = apiResponse.Content.ReadAsStringAsync().Result;
        Console.WriteLine(apiResponseContent);
    }

    public static string GetTokenForApplication(string authority, string clientId, string clientSecret, string resourceUrl)
    {
        AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);
        ClientCredential clientCred = new ClientCredential(clientId, clientSecret);
        AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resourceUrl, clientCred);
        string token = authenticationResult.AccessToken;
        return token;
    }

推薦答案

您的問題與有效受眾有關(guān).您可能有 2 個選擇:

Your problem have something to do with the valid audiences. You may have 2 choices:

選項 1. 嘗試使用 WebAPI 客戶端 ID 作為 AcquireToken 方法的資源"參數(shù)而不是其 Uri 獲取令牌.

Option 1. Try to acquire the token with the WebAPI client ID as the AcquireToken method 'resource' parameter, instead of its Uri.

選項 2.如果以前的方法不起作用,您應(yīng)該使用 Azure 資源資源管理器.導航到您的 Web API,在 config 節(jié)點下找到 authSettings JSON 文檔,并修改(在更改為讀/寫模式后)數(shù)組 allowedAudiences以滿足您的需求.在您的情況下,您可能需要將 http 更改為 https

Option 2. If the previous method didn't work, you should have to modify the authentication settings of the App Service API, using Azure Resources Explorer. Navigate to your web API, find the authSettings JSON document under the config node, and modify (after having changed to Read/Write mode) the array allowedAudiences to match your needs. In your case you may have to change http to https

這篇關(guān)于使用 AAD 對 Azure API 應(yīng)用程序進行身份驗證時出現(xiàn) 401 錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權(quán)不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務(wù)器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發(fā)技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調(diào)用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 久久欧美高清二区三区 | 欧美一区二区三区高清视频 | 蜜桃黄网 | 日韩国产高清在线观看 | 中文天堂网 | 日韩有码一区 | 精品亚洲一区二区 | 在线观看视频h | 男女羞羞视频在线免费观看 | 欧美亚洲在线视频 | 欧美一区免费 | 日韩中文字幕在线 | 精品一区二区三区在线视频 | 91精品国产欧美一区二区成人 | 精品国产一区二区三区久久狼黑人 | 国产97视频在线观看 | 亚洲精品成人 | 亚洲国产精品99久久久久久久久 | 欧美日韩精品免费观看 | 久久久美女 | 国产美女久久 | 日本又色又爽又黄的大片 | 国产精品久久久久久久久久免费看 | 国产精品久久久久久婷婷天堂 | 天天拍天天草 | 亚洲视屏| 国产视频精品区 | www.日韩高清 | 日韩一区二区在线免费观看 | 国产精品视频一 | 欧美天堂在线观看 | 精品国产一区二区三区在线观看 | 福利电影在线 | 免费在线观看91 | 91欧美精品成人综合在线观看 | 免费观看www| 九九热久久免费视频 | 国产精品自产拍在线观看蜜 | 国产精品日日做人人爱 | 在线观看国产网站 | 91xxx在线观看 |