程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了尝试从证书获取访问令牌大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决尝试从证书获取访问令牌?

开发过程中遇到尝试从证书获取访问令牌的问题如何解决?下面主要结合日常开发的经验,给出你关于尝试从证书获取访问令牌的解决方法建议,希望对你解决尝试从证书获取访问令牌有所启发或帮助;

我已经使用我的 Web API 应用程序配置了 Azure AD。 添加了 ClIEnt ID、Certificate、tenantID 所需的配置。 失败的身份验证上下文: 我使用的是 ADAl v5.2.9,authcontext 没有读取诸如 resourcEID、clIEntcredentials 之类的值

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" <using my own API>
$login = "https://login.microsoftonline.com"

# 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";
}

# Output responsE into a JsON file
Invoke-RestMethod -Method Get -Uri ("{0}/{1}/users?API-version=1.6" -f $resourcEID,$tenantID)  -headers $headers -Outfile $output

低于错误

@H_298_5@methodInvocationException: C:\Users\final.ps1:22
line |
  22 |  $authenticationResult = $authContext.Acquiretoken($resourcEID,$clIEnt …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "Acquiretoken" with "2" argument(s): "sts_token_request_Failed: Token request to security token
     | service Failed.  check InnerException for more details"

解决方法

请尝试修改:

$authenticationResult = $authContext.AcquireToken($resourcEID,$clientcredential)
($token = $authenticationResult.AccessToken) | Out-File $accessToken

到:

$authenticationTask = $authContext.AcquireTokenAsync($resourcEID,$clientcredential)
$authenticationTask.Wait()
$authenticationResult = $authenticationTask.Result
($token = $authenticationResult.AccessToken) | Out-File $accessToken

大佬总结

以上是大佬教程为你收集整理的尝试从证书获取访问令牌全部内容,希望文章能够帮你解决尝试从证书获取访问令牌所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。