簡體   English   中英

我在使用托管服務標識對使用 AAD 保護的 Azure 應用服務進行身份驗證時遇到問題

[英]I'm having problems authenticating with Managed Service Identity to an Azure App Service secured with AAD

我正在嘗試使用 HttpClient 連接到托管在 Azure 應用服務中的 Web 服務,該服務使用托管服務標識通過 AAD 進行保護。 嘗試使用 AzureServiceTokenProvider 從客戶端代碼連接時,嘗試獲取令牌時出現 AzureServiceTokenProviderException

基礎設置:

AAD 租戶 AADX.onmicrosoft.com 包含目標應用服務 mytest 的應用程序注冊。 它有應用程序 ID XXXXXXXX-XXXX-49e6-a806-5440b00282b1,根據清單,它有標識符 URL“ https://AADX.onmicrosoft.com/mytest

應用服務“mytest”已在此 AAD 下的訂閱中創建,因此 URL https://mytest.azurewebsites.net

在應用服務的身份驗證設置中,“應用服務身份驗證”已開啟。 請求未通過身份驗證時采取的操作設置為“使用 Azure Active Directory 登錄”。 AAD 身份驗證提供程序配置有快速設置,指向 AADX 並使用“mytest”應用程序。

我們有資源需要在我們的 Azure 租戶內部和外部與此 App 服務對話。 azure 租戶外部的資源通過 API 管理路由到他們需要的 API 的特定部分,以受控方式使用訂閱密鑰。

有應用程序服務和托管在 RemoteApp 中的桌面應用程序需要訪問托管在應用程序下的完整服務。 為租戶內需要連接的所有基礎設施啟用托管身份。

當我使用下面的代碼時,我收到有關無法獲取令牌的異常。 如果我將請求的資源更改為“ https://login.microsoftonline.com/ ”,那么我可以獲得一個令牌,盡管不是正確的(它實際上是獲取我的 MS 在線登錄名,而不是在選項中選擇的帳戶)

        private static HttpClient ConnectToClient()
        {
            String BaseUrl = "https://mytest.azurewebsites.net/";
            String AdResource = "https://AADX.onmicrosoft.com/mytest";

            AzureServiceTokenProvider TokenProvider = new AzureServiceTokenProvider();
            String Token = TokenProvider.GetAccessTokenAsync(AdResource).Result;

            HttpClient Client = new HttpClient()
            {
                BaseAddress = new Uri(BaseUrl)
            };

            Client.DefaultRequestHeaders.Accept.Clear();
            Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


            Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);

            return Client;
        }

        private static String GetContent()
        {
            String Output = String.Empty;

            using (HttpClient Client = ConnectToClient())
            {
                HttpResponseMessage ResponseMessage = Client.GetAsync("api/Test/").Result;

                if (ResponseMessage.IsSuccessStatusCode)
                {
                    Output = ResponseMessage.Content.ReadAsStringAsync().Result;
                }
            }

            return Output;
        }

我希望 Token 作為有效值返回以用作不記名令牌。 相反,在調用 GetAccessTokenAsync 時會拋出以下異常:

System.AggregateException HResult=0x80131500 消息=發生一個或多個錯誤。 (參數:連接字符串:[未指定連接字符串],資源: https : //AADX.onmicrosoft.com/mytest ,權限:.異常消息:嘗試了以下 3 種方法來獲取訪問令牌,但沒有一個有效。

參數:連接字符串:[未指定連接字符串],資源: https : //AADX.onmicrosoft.com/mytest ,權限:。 異常消息:嘗試使用托管服務標識獲取令牌。 無法連接到托管服務標識 (MSI) 端點。 請檢查您是否在具有 MSI 設置的 Azure 資源上運行。 參數:連接字符串:[未指定連接字符串],資源: https : //AADX.onmicrosoft.com/mytest ,權限:。 異常消息:嘗試使用 Visual Studio 獲取令牌。 無法獲取訪問令牌。 Visual Studio 令牌提供程序 Microsoft.Asal.TokenService.exe 的異常:TS003:錯誤,TS004:無法獲取訪問令牌。 '未能刷新訪問令牌'

AzureServiceTokenProvider 在 Azure 上部署時使用托管標識。 當不在 Azure 上時,它確實支持其他身份驗證選項。 您可以使用在環境變量中指定的顯式服務主體憑據,而無需更改代碼。 請參閱此處的文檔。

每個 Visual Studio 版本都提供自己的 Microsoft.Asal.TokenService.exe 版本,並將其反映在 %LOCALAPPDATA%.IdentityService\\AzureServiceAuth\\tokenprovider.json 配置文件中。 請驗證“路徑”中的擴展文件夾。

例如,對於 Visual Studio 16.4 版,擴展位於文件夾“ys2aolp3.2rs”中:

{ 
  "TokenProviders": [
    { 
      "Path": "c:\\program files (x86)\\microsoft visual studio\\2019\\enterprise\\common7\\ide\\extensions\\ys2aolp3.2rs\\TokenService\\Microsoft.Asal.TokenService.exe",
      "Arguments": [
        "--serviceHubConfig", 
        "\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\Common7\\servicehub.config.json\""
      ], 
      "Preference": 0
    }
  ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM