gpt4 book ai didi

Azure .NET SDK - 列出所有虚拟机,无法进行身份验证

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

使用新的 Windows Azure SDK for .NET ,我想获取所有虚拟机的列表。

将早期文档拼凑在一起,这就是我得出的结论:

// This is a custom cert I made locally. I uploaded it to Azure's Management Certificates and added it to my local computer's cert store, see screenshot below.
X509Certificate2 myCustomCert = await this.GetAzureCert();
var credentials = new CertificateCloudCredentials(mySubscriptionId, myCustomCert);

using (var cloudServiceClient = CloudContext.Clients.CreateCloudServiceManagementClient(credentials))
{
credentials.InitializeServiceClient(cloudServiceClient); // Is this required? The next call fails either way.

// This line fails with exception: "The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription."
var services = await cloudServiceClient.CloudServices.ListAsync(CancellationToken.None);
}

我的第一个想法是证书有问题,但我能够使用自定义证书成功调用 Azure REST API。如下所示,它已正确添加到 Azure 管理证书并与我的订阅关联:

enter image description here

我做错了什么?

最佳答案

这里有另一个选择 - 不要上传证书,而是尝试从publishsettings文件中提取管理证书并使用采用字节[]的X509Certificate构造函数。然后,将该参数传递给 Convert.FromBase64String 的调用结果,并将其传递给publishsettings 文件中管理证书的字符串表示形式。

此外,请查看计算管理客户端而不是云服务管理客户端。目前该客户端中还有更多特定于计算堆栈的功能。下面的代码演示了这种方法。请注意,我的 _subscriptionId 和 _managementCert 字段都是字符串,我只是将它们硬编码为我的publishsettings 文件中的值,如上所述。

public async static void ListVms()
{
using (var client = new ComputeManagementClient(
new CertificateCloudCredentials(_subscriptionId,
new X509Certificate2(Convert.FromBase64String(_managementCert)))
))
{
var result = await client.HostedServices.ListAsync();
result.ToList().ForEach(x => Console.WriteLine(x.ServiceName));
}
}

关于Azure .NET SDK - 列出所有虚拟机,无法进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21146731/

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