Question

Using the new Windows Azure SDK for .NET, I want to get a list of all virtual machines.

Piecing together the early documentation, here's what I came up with:

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

My first thought was the cert was bad, but I am able to successfully call the Azure REST API using my custom certificate. As you can see below, it is properly added to the Azure Management Certificates and associated with my subscription:

enter image description here

What am I doing wrong?

Was it helpful?

Solution

Here's another option - rather than upload a cert, try pulling your management cert out of your publishsettings file and using the X509Certificate's constructor that takes a byte[]. Then, pass that parameter the result of a call to Convert.FromBase64String, passing it the string representation of your management certificate from your publishsettings file.

Also, take a look at the Compute management client rather than the Cloud Service Management client. There are more features specific to the compute stack in that client at this time. The code below is a demonstration of such an approach. Note, my _subscriptionId and _managementCert fields are both strings, and I just hard-code them to the values from my publishsettings file as I described above.

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

OTHER TIPS

There's a parameterless ListAsync method that's an extension method. Try importing the Microsoft.WindowsAzure.Management namespace (or the Microsoft.WindowsAzure.Management.Compute namespace). Once you see the parameterless ListAsync method you should be good. I'll also mock up some code to resemble what you're trying to accomplish and offer up a more comprehensive answer by the end of the day.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top