如何访问GRAPH API以使所有用户无需登录?

时间:2016-05-03 20:41:07

标签: azure sharepoint azure-active-directory azure-ad-graph-api

我想要的很简单,但我没有找到明确的答案。

我有一个简单的控制台应用程序,我想要做的就是使用新的GRAPH API获取Azure AD中的所有用户。我所有的例子都需要程序登录(OAuth?)。我不希望这样。我想给代码用户/ pw,然后简单地开始调用方法。

2 个答案:

答案 0 :(得分:1)

每当您有一个用户实际坐在设备上时,到目前为止,您最好的选择是调用完整的登录流程。不仅要保持管理员的凭据不被公开,而且还允许用户在需要时更改密码,调用多因素身份验证等。

但是,在某些情况下,您希望在完全安全且受信任的计算机上运行完全无监督的服务。 (在OAuth 2.0中称为“机密客户端”。)这可以通过OAuth 2.0客户端凭据授权流程实现,该流程仅使用 应用程序的凭据进行身份验证。这在Service to Service Calls Using Client Credentials中有说明。

使用ADAL,可以使用AuthenticationContext.AcquireToken(String, ClientCredential)(您的凭据是密码凭据 - 字符串)或AuthenticationContext.AcquireToken(String, ClientAssertionCertificate)(您的凭据是用于签署的证书)调用此流程一个断言)。 Azure AD samples for daemon applications上的每一个都有一个.NET(C#)示例:

使用PowerShell和证书身份验证,它看起来像这样:

$appId          = "<app client ID>"
$resource       = "https://graph.windows.net" # (or other resource URI)
$tenantId       = "<domain name or ID>"
$certThumbprint = "<certificate thumbprint>"

# Get locally-installed cert by thumbprint
$x509cert = Get-ChildItem "Cert:\LocalMachine\My" | ? { $_.Thumbprint -eq $certThumbprint } | Select-Object -First 1

# Get access token using ClientAssertionCertificate
$authority = "https://login.microsoftonline.com/$tenantId"
$creds = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientAssertionCertificate $appId, $x509cert
$authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext $authority
$authResult = $authContext.AcquireToken($resource, $creds)

# Make Graph API request to list all users
$header = @{
    "Authorization" = "Bearer $($authResult.AccessToken)"
    "Content-Type" = "application/json"
}
$result = Invoke-RestMethod -Method Get -Headers $header -Uri "https://graph.windows.net/$tenantId/users?api-version=1.6"
($result.Content | ConvertFrom-Json).value

您需要确保您的应用程序在Azure AD中注册,并且具有您尝试执行的操作所需的最低应用程序权限(并且不会超过此限制,以便在应用程序的凭据被泄露时限制您的风险)。例如,如果您的应用程序只需要读取目录数据(例如,通过电子邮件地址查找用户),您可以设置如下权限:

App-only permissions to read directory data

答案 1 :(得分: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。