gpt4 book ai didi

c# - 使用 Azure 发布设置文件获取存储客户端凭据

转载 作者:行者123 更新时间:2023-12-03 01:11:49 25 4
gpt4 key购买 nike

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

如何用 C# 完成它?

最佳答案

我在下面编写了一些代码并验证了它是否有效。它基于韦德的帖子:Programmatically Installing and Using Your Management Certificate with the New .publishsettings File 。然后我调用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();

关于c# - 使用 Azure 发布设置文件获取存储客户端凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15481313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com