2015-06-22 194 views
4

我正在嘗試使用Azure資源管理器.Net庫編寫獨立程序來訪問Azure資源組詳細信息。按照文檔要求,每個資源管理器請求中都需要Azure AD身份驗證和令牌。所以我在AD中創建了一個Web應用程序並配置了密鑰並使用它來生成令牌。Azure資源管理器.Net API無法獲取資源組

但下面的代碼失敗,即使我通過這個令牌作爲承載的請求。

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'. 

回答

2

你承載令牌是有效的,但你也需要授予資源組的應用程序訪問。

您可以用下面的PowerShell命令做到這一點:

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

如果您使用的是Azure中的PowerShell版本< 1.0,則該cmdlet是New-AzureRoleAssignment

我建議Dushyant Gill's blog post驗證ARM請求。

+0

非常感謝BenV。我是Azure的新手。你的建議已經解決了這個問題。 – pravn757

+0

我試過這個,但是當我執行這個命令時,我在提示中又遇到了同樣的錯誤。 – Anand

+1

更新:在MS Azure Powershell版本1.0.1中,新AzureRoleAssignment現在是新AzureRmRoleAssignment – hcg