2

當前,當我有一個新環境的請求時,我正在手動在Azure Active目錄上創建我的應用程序。我正在探索通過REST API從代碼創建這些應用程序的方法。通過使用'client_credentials',我成功地在現有應用程序上創建用戶和組,如圖所示。如何以編程方式在Azure中創建應用程序AD

ClientCredential clientCred = new ClientCredential(clientID, clientSecret); 
AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(resAzureGraphAPI, clientCred); 

以類似的方式我試圖用從上面的「ACCESS_TOKEN」中產生以創建一個新的應用程序 adClient.Applications.AddApplicationAsync(newApplication).Wait()

但這引發無錯誤「沒有足夠的權限來完成操作。「

我查看了其他線程和Azure AD msdn頁面,結果發現client_credentials流程不支持創建/更新應用程序。

Adding Applications programmatically in Azure AD using Client Credentials Flow

上述線程還提到的方式來解決辦法是通過使用「grant_type =密碼」流動。我按照建議嘗試了,但是我不斷收到以下錯誤,這對我沒有任何意義。

"error": "invalid_grant", 
    "error_description": "AADSTS50034: To sign into this application the account must be added to the 1283y812-2u3u-u293u91-u293u1 directory.\r\nTrace ID: 66da9cf9-603f-4f4e-817a-cd4774619631\r\nCorrelation ID: 7990c26f-b8ef-4054-9c0b-a346aa7b5035\r\nTimestamp: 2016-02-21 23:36:52Z", 
    "error_codes": [ 
     50034 
    ], 

這是有效載荷和我打的端點。傳遞的用戶是我想創建應用程序的AD的所有者

endpoint:https://login.windows.net/mytenantID/oauth2/token 

post data 
resource 00000002-0000-0000-c000-000000000000 
client_id id 
client_secret secret 
grant_type password 
username [email protected] 
password password 
scope  openid 

任何有關我可能會出錯的想法或建議將不勝感激。

回答

0

您可以使用PowerShell創建您的應用程序:

$servicePrincipalName =」Your Client App Name」 
$sp = New-MsolServicePrincipal -ServicePrincipalNames $servicePrincipalName -DisplayName $servicePrincipalName -AppPrincipalId 「Your Client ID" 
New-MsolServicePrincipalCredential -ObjectId $sp.ObjectId -Type Password -Value 「Your client secret」 
Add-MsolRoleMember -RoleObjectId 「62e90394-69f5-4237-9190-012177145e10" -RoleMemberType ServicePrincipal -RoleMemberObjectId $sp.ObjectId 

通過62e90394-69f5-4237-9190-012177145e10表示的角色是管理角色,這樣就可以根據需要任意的ObjectId調整其他角色。運行Get-MsolRole以獲取角色和ObjectId的列表。

然後,您可以從您的應用程序運行此代碼或手動運行它。您還需要上面,沿着東西的行之前運行連接代碼:

$loginAsUserName = "Your Tenancy Admin Account" 
$loginAsPassword = "Your Tenancy Admin Account Password" 

$secpasswd = ConvertTo-SecureString $loginAsPassword -AsPlainText -Force 
$creds = New-Object System.Management.Automation.PSCredential ($loginAsUserName, $secpasswd) 

Connect-MsolService -Credential $creds 
+0

我第一次用'連接,MsolService'登錄連接。 'Get-MsolRole'列出了一堆角色和他們的名字。然後我想檢查我的角色。所以我解僱了'Get-MsolUserRole -UserPrincipalName test @ mydomain.com'。這返回空。當我開始創建'New-MsolServicePrinicpal'時,我總是得到錯誤 - _Access被拒絕。您無權訪問此cmdlet._ – Srikanth

0

我能在我的房客來創建應用程序。我用來創建應用程序的AD租戶已針對不同的域進行了驗證。基本上,我結束了從該域中的用戶插入,並使用resource_type =密碼流能夠生成訪問令牌。接下來,發射下面的代碼行的伎倆

ActiveDirectoryClient adClient = new ActiveDirectoryClient(
       serviceRoot, 
       AccessToken); 
adClient.Applications.AddApplicationAsync(newApplication).Wait(); 
0

檢查下面的事情似乎有點偏離你POST到OAuth令牌端點:

  • 如果想訪問圖表您的Azure AD的API,您需要通過https://graph.windows.net作爲resource身體參數;這是(imho)沒有很好的記錄,但這就是你需要做的
  • 由於client_idclient_secret你需要傳遞你的Azure AD中的預定義應用程序的客戶端ID和密鑰,這反過來你已經授予了每個用戶級別;這些都需要足夠添加應用程序
  • 不使用scope參數,我想;你會得到你在Azure AD管理門戶中定義的回報,這應該使您的訪問索賠(爲您的應用分配權限)

令牌可以隨後在https://graph.windows.net/tenantId/末點使用。

希望幫助, 馬丁

相關問題