0

我想要的很簡單,但我還沒有找到明確的答案。如何訪問GRAPH API讓所有用戶無需登錄?

我有一個簡單的控制檯應用程序和所有我想要做的就是在我的Azure的廣告中使用了新的圖形API的用戶。我所有的例子都需要程序登錄(OAuth?)。我不想那樣。我想給代碼提供user/pw,並簡單地開始調用這些方法。

+0

即使你有用戶/ passwd文件,天青仍然需要登錄驗證,請嘗試'UserCredential userCred = new UserCredential(User,Password);' –

+0

我是否需要在Azure中'創建應用程序'? – punkouter

+0

不,僅使用用戶憑證 –

回答

0

這是我寫的PowerShell腳本。

# Adding the AD library to your PowerShell Session. 
Add-Type -Path 'C:\Program Files\Microsoft Azure Active Directory Connect\Microsoft.IdentityModel.Clients.ActiveDirectory.dll' 

# This is the tenant id of you Azure AD. You can use tenant name instead if you want. 
$tenantID = "<the tenant id of Azure AD>" 
$authString = "https://login.microsoftonline.com/$tenantID" 

# Here, the username must be MFA disabled user Admin at least, and must not be a live id. 
$username = "<the username of the AD's Admin>" 
$password = "<the password of the above user>" 

# The resource URI for your token. 
$resource = "https://graph.windows.net/" 

# This is the common client id. 
$client_id = "1950a258-227b-4e31-a9cf-717495945fc2" 

# Create a client credential with the above common client id, username and password. 
$creds = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" ` 
        -ArgumentList $username,$password 

# Create a authentication context with the above authentication string. 
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" ` 
        -ArgumentList $authString 

# Acquire access token from server. 
$authenticationResult = $authContext.AcquireToken($resource,$client_id,$creds) 

# Use the access token to setup headers for your http request. 
$authHeader = $authenticationResult.AccessTokenType + " " + $authenticationResult.AccessToken 
$headers = @{"Authorization"=$authHeader; "Content-Type"="application/json"} 

# Get the users. 
Invoke-RestMethod -Method GET -Uri "https://graph.windows.net/$tenantID/users?api-version=1.6" 

如果您使用的是C#,它會非常相似,因爲我的腳本實際上是從C#代碼轉換而來的。對於其他編程語言,相應的Azure SDK中也有類似的API。如果不是,您可以考慮使用OAuth2。

+0

所以我仍然在Azure創建一個應用程序?我不只是使用管理員用戶/密碼?我習慣了以前的做事方式,所以這些令牌/ Oauth概念對我來說都是新的。它在我看來我需要兩個。創建應用程序並授予它在Azure中的權限以及登錄。試試這個嘗試並返回給您 – punkouter

+1

此解決方案將管理用戶的用戶名和密碼存儲在腳本中,這通常被認爲是非常不安全的方法。 –

+0

我將在代碼(dll)中擁有我的代碼。但它不是一個非常嚴肅的產品。只需要它的工作。 – punkouter

1

只要你有一個用戶物理坐在設備,最好的辦法,到目前爲止,是調用完整的登錄流程。不僅保持管理員的憑據不被公開,還允許用戶在需要時更改密碼,調用多因素身份驗證等。

但是,在某些情況下,您希望運行完全無監督的服務一個完全安全和可信賴的機器。 (在OAuth 2.0中稱爲「機密客戶端」)。這可以通過OAuth 2.0客戶端憑證授予流程來實現,該流程僅使用該應用程序的憑證進行身份驗證。這在Service to Service Calls Using Client Credentials中說明。

使用ADAL,通過使用AuthenticationContext.AcquireToken(String, ClientCredential)(其中您的憑證是密碼憑證 - 字符串)或AuthenticationContext.AcquireToken(String, ClientAssertionCertificate)(其中您的憑證是用於簽署斷言的證書)來調用此流程。有一個.NET(C#)樣品每一種的Azure AD samples for daemon applications

使用PowerShell和證書認證,這將是這個樣子:

你需要確保你的應用程序在Azure的AD註冊,並有所需的最低applicati對你正在嘗試做什麼的權限(以及不超過這個限制,如果應用程序的憑據被泄露,限制你的暴露)。例如,如果你的應用程序只需要讀取目錄數據(例如查找電子郵件地址的用戶),您將設置權限,就像這樣:

App-only permissions to read directory data

+0

在我的情況下,用戶在外部程序中,當他們按下按鈕時,我的代碼就會出現。當按下按鈕時,我使用Graph API發送電子郵件並將電子郵件與AAD用戶進行匹配。因爲他們沒有權限(或者你設置了Azure應用程序的權限?),所以我認爲這不會有意義,甚至無法與用戶在進行Office365登錄時一起工作。但是也許我錯過了東西 – punkouter

+0

這真的取決於你需要什麼。 @PhilippeSignoret提供的代碼要求您創建一個帶證書的AD應用程序,正確配置權限,並讓客戶將證書安裝到他們的機器中。而且,它將無法爲您創建新的應用程序AD。它無法創建新用戶(如果您使用的是b2c,則情況並非如此)。它更安全,因爲您可以完全控制AD應用程序和證書的權限。 –

+0

@punkouter當然,有些情況下您想在僅應用程序環境中調用Azure AD Graph API,這就是客戶端憑據流存在的原因。是的,當你配置應用程序,你會選擇應用程序應該有什麼權限(我添加了截圖到我的答案)。 –

相關問題