How can I authenticate to AAD and call the Graph API as a Daemon Application with PowerShell?

后端 未结 1 1068
时光取名叫无心
时光取名叫无心 2021-01-29 07:27

I am trying to do some very quick tests on Azure Active Directory, and I want to use a Daemon Application to access the Graph API without needing a user present to authenticate.

相关标签:
1条回答
  • 2021-01-29 07:47

    This question is very similar to this one where create a PowerShell script to authenticate as a Native Client Application. However, in this situation, there are some subtle and important differences because you want to authenticate as a confidential client. Specifically, we need to create a Client Credential so that we can authenticate without a user as a Daemon Application.

    First you need to download and save the .NET dlls for ADAL. The download link can be found on Nuget.

    Note: We specifically use ADAL v2 here.

    You can extract the contents of the .nupkg with a File Extractor like 7z, WinZip, etc...

    Extract the contents from \lib\net45\ and copy them into your working directory. I put the files in their own "ADAL" folder, to keep it separate.

    Then you should be able to create a new PowerShell script with the following:

    # Load ADAL
    Add-Type -Path ".\ADAL\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
    
    # Output Token and Response from AAD Graph API
    $accessToken = ".\Token.txt"
    $output = ".\Output.json"
    
    # Application and Tenant Configuration
    $clientId = "<AppIDGUID>"
    $tenantId = "<TenantID>"
    $resourceId = "https://graph.windows.net"
    $login = "https://login.microsoftonline.com"
    
    # Create Client Credential Using App Key
    $secret = "<AppKey>"
    
    
    # Create Client Credential Using Certificate
    #$certFile = "<PFXFilePath>"
    #$certFilePassword = "<CertPassword>"
    #$secret = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate -ArgumentList $certFile,$certFilePassword
    
    
    # Get an Access Token with ADAL
    $clientCredential = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential($clientId,$secret)
    $authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("{0}/{1}" -f $login,$tenantId)
    $authenticationResult = $authContext.AcquireToken($resourceId, $clientcredential)
    ($token = $authenticationResult.AccessToken) | Out-File $accessToken
    
    
    # Call the AAD Graph API 
    $headers = @{ 
        "Authorization" = ("Bearer {0}" -f $token);
        "Content-Type" = "application/json";
    }
    
    Invoke-RestMethod -Method Get -Uri ("{0}/{1}/users?api-version=1.6" -f $resourceId,$tenantId)  -Headers $headers -OutFile $output
    

    Note: You will need to update the App ID, Tenant ID, and your App Secret information in this script. If you use a certificate to authenticate, simply comment out the code that uses the App Key, and un-comment the code which uses the certificate. I have also pre-configured the AAD Graph API call to return the users in my tenant, but you can change this REST call to whatever you want.

    After you successfully run the script, you should get 2 new files in your working directory: A text file that contains your encoded JSON access token, which can be base64 decoded on sites like this, and a JSON file with the response from the AAD Graph API.

    Let me know if this helps!

    0 讨论(0)
提交回复
热议问题