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

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

將 AzureCredentialsFactory.FromServicePrincipal

將 AzureCredentialsFactory.FromServicePrincipal

C#
倚天杖 2022-06-12 15:12:35
我使用 .NET 框架 4.7.2 創建了一個控制臺應用程序,以使用這些 nuget 包連接到 Azure 資源管理器 API:Microsoft.Azure.Management.ResourceManager.Fluent:v1.18.0 | 下載鏈接Microsoft.Azure.Management.Fluent:v1.18.0 | 下載鏈接這是代碼:namespace AzResourceManager{    class Program    {        static void Main(string[] args)        {            var clientId = "********-****-****-****-************";            var subscriptionId = "********-****-****-****-************";            var tenantId = "********-****-****-****-************";             var cert = GetCertificate("********************************");            var creds = new AzureCredentialsFactory().FromServicePrincipal(clientId, cert, tenantId, AzureEnvironment.AzureGlobalCloud);            var azure = Azure.Authenticate(creds).WithSubscription(subscriptionId);            foreach (var rGroup in azure.ResourceGroups.List())            {                Console.WriteLine(rGroup.Name);            }        }        private static X509Certificate2 GetCertificate(string thumbPrint)        {            var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);            certStore.Open(OpenFlags.ReadOnly);            try            {                var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbPrint, false);                if (certCollection.Count <= 0)                    throw new InvalidOperationException("Unable to load certificate from store");                return certCollection[0];            }            finally            {                certStore.Close();            }        }    }}當執行到達這一行時foreach (var rGroup in azure.ResourceGroups.List())應用程序拋出一個System.NullReferenceException: 'Object reference not set to an instance of an object.'我在 Azure Active Directory 和筆記本電腦中注冊的應用程序中安裝了證書。我嘗試更新一些軟件包,但結果是一樣的。我唯一無法更新到最新版本的軟件包是Microsoft.IdentityModel.Clients.ActiveDirectory(最新版本:v4.4.1),我只能將其更新到版本 v3.19.8
查看完整描述

2 回答

?
12345678_0001

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

您是否能夠使用Resource Groups List REST API獲得結果。我已經嘗試過自己,并且在使用您的代碼時沒有收到任何錯誤,并且對我來說效果很好。請確保已為您注冊的應用程序服務主體提供了至少對您的訂閱的讀者訪問權限。請按照文檔使用 RBAC 和 Azure 門戶管理訪問權限為 Azure 資源分配角色。



查看完整回答
反對 回復 2022-06-12
?
喵喔喔

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

嘗試使用帶有.CER擴展名的導出證書時,我得到同樣令人沮喪的 System.NullReferenceException,但是在使用密碼以.PFX格式導出證書時:重載的 FromServicePrincipal 函數成功


var credentials = SdkContext

        .AzureCredentialsFactory

        .FromServicePrincipal(

             "********-****-****-****-************", // clientId

              @"c:\cert.pfx", // certificate file path

             "123456", // certificate password

             "********-****-****-****-************", //tenantId

             AzureEnvironment.AzureGlobalCloud);


查看完整回答
反對 回復 2022-06-12
  • 2 回答
  • 0 關注
  • 150 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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