Jonny Leigh
Jonny Leigh

Reputation: 125

Authenticating to Microsoft.Azure.Management.Automation.AutomationManagementClient without using management certificates

I'm building a dotnet core 2.1 app and attempting to use AutomationManagementClient to pull status on Azure Automation jobs.

I'm using the constructor of AutomationManagementClient which accepts Microsoft.Rest.ServiceClientCredentials (https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.management.automation.automationclient.-ctor?view=azure-dotnet#Microsoft_Azure_Management_Automation_AutomationClient__ctor_Microsoft_Rest_ServiceClientCredentials_System_Net_Http_DelegatingHandler___)

According to the docs (https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.management.resourcemanager.fluent.authentication.azurecredentials?view=azure-dotnet) AzureCredentials is an implementation of Microsoft.Rest.ServiceClientCredentials.

To build the ServiceClientCredentials I use the following code:

AuthenticationContext authContext =
                new AuthenticationContext(string.Format
                ("https://login.windows.net/{0}",
                tenantID));

            AuthenticationResult tokenAuthResult =
                authContext.AcquireTokenAsync(applicationId,
                new ClientCredential(applicationId, authenticationKey)).Result;

            TokenCredentials cred = new TokenCredentials(tokenAuthResult.AccessToken);

            return new AutomationManagementClient(cred);

However on the last line I'm getting the error cannot convert from 'Microsoft.Rest.TokenCredentials' to 'Microsoft.Azure.SubscriptionCloudCredentials'

Any idea what I am doing wrong here?

Thanks,

Upvotes: 0

Views: 1125

Answers (1)

Philippe Signoret
Philippe Signoret

Reputation: 14336

You have two issues:

  1. You will be calling the Azure Management API, so the resource you identify in AcquireTokenAsync should not be your own app ID, but the identifier for the resource you want a token for: https://management.azure.com:

    AuthenticationResult tokenAuthResult = authContext.AcquireTokenAsync(
            "https://management.azure.com",
            new ClientCredential(applicationId, authenticationKey)).Result;    
    
  2. As is mentioned in the error message you cite, the AutomationManagementClient constructor expects an instance of SubscriptionCloudCredentials, not an instance of ServiceClientCredentials (the docs you link to are for AutomationClient, not for AutomationManagementClient). The closest to what you're trying to do would be TokenCloudCredentials (to which you also need to provide the subscription ID):

    TokenCloudCredentials cred = new TokenCloudCredentials(
            subscriptionId, tokenAuthResult.AccessToken);
    

Upvotes: 2

Related Questions