Azure资源管理器.Net API无法获取资源组

时间:2015-06-22 21:17:53

标签: azure-resource-manager

我正在尝试使用Azure资源管理器.Net库编写一个独立程序来访问Azure资源组详细信息。根据文档,它在每个资源管理器请求中都需要Azure AD身份验证和令牌。所以我在AD中创建了一个Web应用程序并配置了密钥并使用它来生成令牌。

但是,即使我在请求中将此令牌作为承载传递,代码也会失败。

m_resourceClient = new ResourceManagementClient(connection.GetCredentials());
m_resourceClient.HttpClient.DefaultRequestHeaders.Authorization = new     AuthenticationHeaderValue("bearer", connection.GetAccessToken());
***ResourceGroupGetResult resourceGroupList = m_resourceClient.ResourceGroups.Get("PraveenTest")*** ;

错误讯息:

AuthorizationFailed: The client '5919f7f9-####-####-####-074456eba98c' with object id '5919f7f9-####-####-####-074456eba98c' does not have authorization to perform action 
'Microsoft.Resources/subscriptions/resourcegroups/read' over scope '/subscriptions/1f94c869-####-####-####-055e8ae15be3/resourcegroups/TestGroup'.

1 个答案:

答案 0 :(得分:2)

您的持有者令牌有效,但您还需要授予您的应用程序访问该资源组的权限。

您可以使用以下PowerShell命令执行此操作:

New-AzureRmRoleAssignment 
   -ObjectId '5919f7f9-####-####-####-074456eba98c' `
   -ResourceGroupName TestGroup `
   -RoleDefinitionName Reader

如果您使用的是Azure PowerShell版本< 1.0,然后cmdlet为New-AzureRoleAssignment

我建议Dushyant Gill's blog post验证ARM请求。