CloudAnywhere
CloudAnywhere

Reputation: 759

Get list of instances in hosted services in Azure

I have a worker role in Azure and I want to list all the instances in C# dotnet core. There are several azure management nuget packages. I have tested a few of them but none gave the expected result. I also tried native rest apis but couldn't find a working sample.

Can someone post a working sample? Ideally, I would like to use a Microsoft nuget library for managing Azure.

Tested so far: CSManage: https://github.com/Plasma/csmanage => It does not work on dotnet core (it's an old WCF model).

Tried using REST: 401 unauthorized Azure management api => I'm getting access denied. (It works only when going through fiddler and I did not find the solution)

Upvotes: 0

Views: 144

Answers (1)

Jim Xu
Jim Xu

Reputation: 23111

Azure cloud service is 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

The detailed steps are as below

  1. Uplaod certificate to Azure a. create a certificate

    $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 upload .cer file to Azure(Subscriptions -> your subscription -> Management certificates) enter image description here

  2. Code

 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));
        }

Upvotes: 1

Related Questions