我正在构建一个dotnet core 2.1应用程序,并尝试使用AutomationManagementClient来获取Azure自动化作业的状态。
我正在使用AutomationManagementClient的构造函数,该构造函数接受Microsoft.Rest.ServiceClientCredentials(https://docs.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___)
根据文档(https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.resourcemanager.fluent.authentication.azurecredentials?view=azure-dotnet),AzureCredentials是Microsoft.Rest.ServiceClientCredentials的实现。
要构建ServiceClientCredentials,请使用以下代码:
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);
但是在最后一行,我得到了错误cannot convert from 'Microsoft.Rest.TokenCredentials' to 'Microsoft.Azure.SubscriptionCloudCredentials'
你知道我在做什么错吗?
谢谢
答案 0 :(得分:1)
您有两个问题:
您将调用Azure管理API,因此您在AcquireTokenAsync
中标识的资源不应是您自己的应用程序ID,而应是您想要令牌的资源的标识符:{{1} }:
https://management.azure.com
正如您引用的错误消息中所提到的,AuthenticationResult tokenAuthResult = authContext.AcquireTokenAsync(
"https://management.azure.com",
new ClientCredential(applicationId, authenticationKey)).Result;
构造函数期望AutomationManagementClient
的实例,而不是SubscriptionCloudCredentials
的实例(链接到的文档仅用于ServiceClientCredentials
,不适用于AutomationClient
)。与您尝试执行的操作最接近的是AutomationManagementClient
(您还需要提供订阅ID):
TokenCloudCredentials