Forked from craig-martin/Invoke-WebApiWithAadToken.ps1
Created
January 26, 2017 14:57
-
-
Save nordineb/a2d206423f0a02fa4e776a13241e807e to your computer and use it in GitHub Desktop.
Use PowerShell to Authenticate to AAD Then Call a Web API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Load ADAL | |
Add-Type -Path "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll" | |
# Set AAD client ID for the client app | |
$clientId = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" | |
$resourceAppIdURI = "https://TestWebApi01.azurewebsites.net" | |
$authority = "https://login.windows.net/MyAadDirectory.onmicrosoft.com" | |
# Create Authentication Context tied to Azure AD Tenant | |
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority | |
# Acquire token http://www.cloudidentity.com/blog/2014/07/10/adal-v2-and-windows-integrated-authentication/ | |
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, (New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential [email protected])) | |
### Use the token to call the API | |
Invoke-RestMethod -Method Get -Uri https://TestWebApi01.azurewebsites.net/api/values -Headers @{"Authorization" = "Bearer $($authResult.AccessToken)"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment