5

我正在尝试编写一个独立程序来使用 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'.
4

1 回答 1

2

您的不记名令牌是有效的,但您还需要授予您的应用程序访问资源组的权限。

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

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

如果您使用的 Azure PowerShell 版本 < 1.0,则 cmdlet 为New-AzureRoleAssignment.

我推荐Dushyant Gill关于验证 ARM 请求的博文。

于 2015-06-22T21:48:34.783 回答