Created
February 2, 2016 20:50
-
-
Save craig-martin/91829dbb26971995626e to your computer and use it in GitHub Desktop.
Use PowerShell to Authenticate to AAD Then Call a Web API
This file contains 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 me@MyAadDirectory.com)) | |
### 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