获取 Azure 托管服务中的实例列表

Get list of instances in hosted services in Azure

我在 Azure 中有一个工作者角色,我想列出 C# dotnet 核心中的所有实例。 有几个 azure management nuget 包。我已经测试了其中的一些,但 none 给出了预期的结果。 我也试过原生 rest api,但找不到工作示例。

有人可以 post 工作样本吗? 理想情况下,我想使用 Microsoft nuget 库来管理 Azure。

目前已测试: CS管理:https://github.com/Plasma/csmanage => 它不适用于 dotnet 核心(它是一个旧的 WCF 模型)。

尝试使用 REST:401 unauthorized Azure management api => 我的访问被拒绝。 (只有通过fiddler才有效,我没有找到解决方法)

Azure 云服务是 Azure classic resource. So we need to use Azure service management API to manage it. If we want to call the API, we need to do X509 client certificates authentication. For more details, please refer to the document

详细步骤如下

  1. 将证书上传到 Azure 一种。创建证书

    $cert = New-SelfSignedCertificate -DnsName yourdomain.cloudapp.net -CertStoreLocation "cert:\LocalMachine\My" -KeyLength 2048 -KeySpec "KeyExchange"
    $password = ConvertTo-SecureString -String "your-password" -Force -AsPlainText
    Export-PfxCertificate -Cert $cert -FilePath ".\my-cert-file.pfx" -Password $password
    Export-Certificate -Type CERT -Cert $cert -FilePath .\my-cert-file.cer
    

    b 将 .cer 文件上传到 Azure(订阅 -> 您的订阅 -> 管理证书)

  2. 代码

 static async Task Main(string[] args)
 {
   var _clientHandler = new HttpClientHandler();
                _clientHandler.ClientCertificates.Add(GetStoreCertificate("the cert's thumbprint" ));
                _clientHandler.ClientCertificateOptions = ClientCertificateOption.Manual;
                String uri = string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}?embed-detail=true", "subscription id","<could service name>");
                using (var _client = new HttpClient(_clientHandler))
                using (var request = new HttpRequestMessage(HttpMethod.Get, uri)) {

                    request.Headers.Add("x-ms-version", "2014-05-01");
                    request.Headers.Add("Accept", "application/xml");
                    //request.Headers.Add("Content-Type", "application/xml");
                    using (HttpResponseMessage httpResponseMessage = await _client.SendAsync(request)) {
                        string xmlString = await httpResponseMessage.Content.ReadAsStringAsync();
                        Console.WriteLine(httpResponseMessage.StatusCode);
                    }

                }
}
private static X509Certificate2 GetStoreCertificate(string thumbprint)
        {


            X509Store store = new X509Store("My", StoreLocation.LocalMachine);
            try
            {
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                X509Certificate2Collection certificates = store.Certificates.Find(
                  X509FindType.FindByThumbprint, thumbprint, false);
                if (certificates.Count == 1)
                {
                    return certificates[0];
                }
            }
            finally
            {
                store.Close();
            }

            throw new ArgumentException(string.Format(
              "A Certificate with Thumbprint '{0}' could not be located.",
              thumbprint));
        }