1

我有 Azure 发布设置文件。现在我必须在订阅中访问具有指定名称的存储帐户。

如何在 C# 中完成它?

4

1 回答 1

3

我在下面写了一些代码并验证它是否有效。它基于 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();
于 2013-03-18T18:12:50.780 回答