Reputation: 125
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
Reputation: 14336
You have two issues:
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;
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