簡體   English   中英

使用Azure publishsettings文件獲取存儲客戶端憑據

[英]Getting storage client credentials with Azure publishsettings file

我有Azure publishsettings文件。 現在我必須訪問訂閱中具有指定名稱的存儲帳戶。

如何在C#中完成它?

我在下面寫了一些代碼並驗證它是否有效。 它基於Wade的帖子: 以新的.publishsettings文件以編程方式安裝和使用您的管理證書 然后我調用Get Storage Account Keys方法。 Wade的帖子中提到的幾個指針:最好創建一個證書並在本地安裝它,然后使用它來調用SM API,以便您可以刪除.publishsettings文件。 它包含您的SM API證書信息,因此您應該刪除它或保證它的安全。 為簡潔起見,此代碼不執行安裝位,但是Wade的帖子有它。

        var publishSettingsFile =
        @"C:\yourPublishSettingsFilePathGoesHere";

        XDocument xdoc = XDocument.Load(publishSettingsFile);

        var managementCertbase64string =
            xdoc.Descendants("PublishProfile").Single().Attribute("ManagementCertificate").Value;

        var managementCert = new X509Certificate2(
            Convert.FromBase64String(managementCertbase64string));

        // If you have more than one subscription, you'll need to change this
        string subscriptionId = xdoc.Descendants("Subscription").First().Attribute("Id").Value;
        string desiredStorageService = "yourStorageServiceName";

        var req = (HttpWebRequest)WebRequest.Create(
            string.Format("https://management.core.windows.net/{0}/services/storageservices/{1}/keys",
                                            subscriptionId,
                                            desiredStorageService));
        req.Headers["x-ms-version"] = "2012-08-01";
        req.ClientCertificates.Add(managementCert);

        XNamespace xmlns = "http://schemas.microsoft.com/windowsazure";

        XDocument response = XDocument.Load(req.GetResponse().GetResponseStream());

        Console.WriteLine("Primary key: " + response.Descendants(xmlns + "Primary").First().Value);
        Console.WriteLine("Secondary key: " + response.Descendants(xmlns + "Secondary").First().Value);

        Console.Read();

暫無
暫無

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

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