【问题标题】:AcquireTokenAsync not found for auth context when trying to get token尝试获取令牌时未找到身份验证上下文的 AcquireTokenAsync
【发布时间】:2016-12-13 23:11:26
【问题描述】:

我正在尝试让不记名令牌通过这样的 powershell 脚本调用 API:

function GetAuthToken
{
    param
    (
        [Parameter(Mandatory=$true)]
        $ApiEndpointUri,

        [Parameter(Mandatory=$true)]
        $AADTenant
)
$adal = "C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\" + `
            "Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = "C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\" + `
                "Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"

[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
[System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null

$clientId = "1950a258-227b-4e31-a9cf-717495945fc2"
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$authorityUri = “https://login.windows.net/$aadTenant”

$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authorityUri



$authResult = $authContext.AcquireTokenAsync($ApiEndpointUri, $clientId,$redirectUri, "Auto")
$authResult.Wait();

return $authResult
$ApiEndpointUri = "https://management.azure.com/" #change this to graph api uri
$AADTenant = 'GUID' #AAD tenant guid
$token = GetAuthToken -ApiEndPointUri $ApiEndpointUri -AADTenant $AADTenant
$header = @{
'Content-Type'='application\json'
'Authorization'=$token.CreateAuthorizationHeader()
}

$request = ``
(Invoke-RestMethod -Uri $request -Headers $header -Method Get).value

但是,acquiretokenasync 失败并出现此错误:

Cannot find an overload for "AcquireTokenAsync" and the argument count: "4".

任何想法为什么我会遇到这个问题? AcquireTokenAsync 采用我所知道的 4 个参数。

【问题讨论】:

    标签: c# powershell authentication oauth token


    【解决方案1】:

    我没有看到带有 4 个参数的 AcquireTokenAsync 重载。您还需要确保正确地转换参数。

    查看过载列表here。您认为您正在使用哪些重载?您会注意到所有参数都是字符串,但没有一个重载只接受字符串。

    【讨论】:

    • 嗯,我在 c# 方法中使用了它,它运行良好:Task resultTask = authContext.AcquireTokenAsync((AuthServerAddress, clientId, new Uri("login.live.com/oauth20_desktop.srf"), new Microsoft.IdentityModel .Clients.ActiveDirectory.PlatformParameters(PromptBehavior.Auto, false));
    • 是因为我使用的是版本 3+ 的 ActiveDirectory dll 吗?知道如何将上述内容转换为重载列表中列出的acquiretokenasyncs之一吗?
    【解决方案2】:

    我在 Windows 10 build 1703 上遇到了类似的错误(尽管脚本在 build 1607 上运行)。我安装了 Microsoft.IdentityModel.Clients.ActiveDirectory 版本 3.13.9。我用 version 3.13.8 替换了这些程序集并重新启动了 PowerShell,这清除了我的错误。

    【讨论】:

      猜你喜欢
      • 2020-03-06
      • 2015-09-14
      • 2013-12-04
      • 2015-06-01
      • 2020-07-31
      • 2015-05-10
      • 2019-12-05
      • 2015-11-04
      • 2015-06-21
      相关资源
      最近更新 更多