本文介紹了使用 azure Active Directory 進行身份驗證時調用 Azure 管理庫 API 時出錯的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我的公司正在研究有關 Azure 的報告.我們只希望我們的客戶向我們提供只讀憑據以供我們使用.我做了一些研究,看起來 Azure Active Directory 就是這樣做的.所以我希望使用只讀的 Azure 目錄應用程序進行身份驗證.
為了讓我開始,我關注了這篇關于通過 Azure Active Directory 使用管理 API 的博客.
但是在此 UI 中,我無法為該屬性選擇任何值.我不確定這是錯誤還是未完成功能的結果.我在這里遺漏了什么嗎?
謝謝
這是我的完整代碼供參考:
類程序{靜態無效主要(字符串 [] 參數){var token = GetAuthorizationHeader();var credential = new TokenCloudCredentials(ConfigurationManager.AppSettings["subscriptionId"], token);使用 (var computeClient = new ComputeManagementClient(credential)){var images = computeClient.VirtualMachineOSImages.List();}}私有靜態字符串 GetAuthorizationHeader(){AuthenticationResult 結果 = null;var context = new AuthenticationContext("https://login.windows.net/" + ConfigurationManager.AppSettings["tenantId"]);字符串 clientId = ConfigurationManager.AppSettings["clientId"];字符串 clientSecret = ConfigurationManager.AppSettings["clientSecret"];ClientCredential clientCred = new ClientCredential(clientId, clientSecret);var thread = new Thread(() =>{結果 = context.AcquireToken("https://management.core.windows.net/",客戶信用);});線程.SetApartmentState(ApartmentState.STA);thread.Name = "AquireTokenThread";線程.Start();線程.Join();如果(結果 == 空){throw new InvalidOperationException("獲取 JWT 令牌失敗");}字符串令牌 = result.AccessToken;返回令牌;}}
已經取得了進展.正如我與 Gaurav 討論的那樣,我需要放棄 Azure 管理庫,因為目前它似乎不支持 Azure 資源管理器 (ARM) API!所以我做了原始的網絡請求.它按預期工作.如果我從我的 AD 應用程序中刪除角色訪問權限,我會被拒絕訪問.當我擁有它時,我會取回數據.
我不確定的一件事是讓我的應用程序自動添加到新資源中.
另外,有沒有辦法列出我的 AD 應用程序可以訪問的資源組?
新代碼:
類程序{靜態無效主要(字符串 [] 參數){var token = GetAuthorizationHeader();string subscriptionId = ConfigurationManager.AppSettings["subscriptionId"];string resourceGroupName = ConfigurationManager.AppSettings["resourceGroupName"];var uriListMachines = string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/virtualmachines?api-version=2015-05-01-preview", 訂閱 ID, 資源組名);var t = WebRequest.Create(uriListMachines);t.ContentType = "應用程序/json";t.Headers.Add("授權", "承載" + token);var response = (HttpWebResponse)t.GetResponse();字符串結果 = "";使用 (var reader = new StreamReader(response.GetResponseStream())){結果 = reader.ReadToEnd();}//原始嘗試://var credential = new TokenCloudCredentials(ConfigurationManager.AppSettings["subscriptionId"], token);//使用 (var client = CloudContext.Clients.CreateComputeManagementClient(credential))//{//var images = client.VirtualMachineVMImages.List();/
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!