Stefan
Stefan

Reputation: 585

AuthenticationContex missing method AcquireToken and CreateAuthorizationHeader

I try to work with Azure AD.

This is the code I use:

Add-Type -Path      "D:\GraphAPI\Microsoft.IdentityModel.Clients.ActiveDirectory.2.14.201151115\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll" 


# Change these three values to your application and tenant settings
$clientID = "CLIENT ID for application"
$clientSecret = "KEY for application"
$tenant = "tenant domain name"

# Static values
$resAzureGraphAPI = "https://graph.windows.net";
$serviceRootURL = "https://graph.windows.net/$tenant"
$authString = "https://login.windows.net/$tenant";

# Creates a context for login.windows.net (Azure AD common authentication)
  [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]$AuthContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]$authString

# Creates a credential from the client id and key
[Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]$clientCredential = New-Object -TypeName "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential"($clientID, $clientSecret)

# Requests a bearer token
$authenticationResult = $AuthContext.AcquireTokenAsync($resAzureGraphAPI,     $clientCredential);

# Output the token object
Write-Host -ForegroundColor Yellow "Token object:"
$authenticationResult | Format-List

# Example to get all users
Write-Host -ForegroundColor Yellow "Getting all users"
$users = Invoke-RestMethod -Method GET -Uri "$serviceRootURL/users?api-version=1.5" -Headers @{Authorization=$authenticationResult.CreateAuthorizationHeader()} -ContentType "application/json"
$users.value | Format-Table UserPrincipalName,DisplayName

But I have two errors

Method invocation failed because [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContex] does not contain a method named ‘AcquireToken’

Method invocation failed because [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContex] does not contain a method named ‘CreateAuthorizationHeader’

can you please help me to solve this errors?

Thanks or your help

Stefan

Upvotes: 4

Views: 4607

Answers (3)

Ashutosh B Bodake
Ashutosh B Bodake

Reputation: 1412

string token =authContext.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri), new PlatformParameters(0)).Result.AccessToken;

More information on below link:- https://github.com/Microsoft/PowerBI-CSharp/issues/32

Upvotes: 0

user3032525
user3032525

Reputation: 1

https://www.nuget.org/packages/microsoft.identitymodel.clients.activedirectory/2.19.208020213

The owner has unlisted this package. This could mean that the package is deprecated or shouldn't be used anymore.

Version doesn't exist ..

Upvotes: 0

MGS
MGS

Reputation: 35

Sorry for the late response, but I just now had the same issue and found your question. I noticed that within Microsoft.IdentityModel.Clients.ActiveDirectory.XML for version 3.10.305231913 (the most recent), these methods were missing entirely, among some others. I grabbed version 2.26.305102204 instead via:

.\nuget.exe install Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.26.305102204

Everything seems to be in place in this older version.

Upvotes: 1

Related Questions