微软身份平台和OAuth 2.0授权

问题描述 投票:0回答:1

我正在尝试使用 OAuth 2.0 通过 API 进行身份验证,以便生成不记名令牌,从而允许我执行 API 调用。这必须在 PowerShell 中完成。

现状:

要执行 API 调用,我目前必须使用 Swagger 授权(如下图)

我通过复选框选择范围并单击授权。

结果:

现在我可以通过 Swagger API 调用生成不记名令牌。有了这个令牌我就可以执行所有必要的操作。

我不想每次都手动创建不记名令牌并将此令牌复制到 PS 脚本中。 使用此脚本将执行所有 API 调用。

这与从上面的操作复制的不记名令牌完美配合。

目前已达到:

我获得了 API 的所有信息,例如 client_id、tenantID、client_secret、scope、redirect_url...

根据这些信息,我用 PS 生成了一个不记名令牌。

脚本:

Connect-AzAccount 

# Collect all tenant
$allTenants = Get-AzTenant

# Filter for right tenant
$targetTenant = $allTenants | Where-Object { $_.Name -eq "XXX" }

# Check if tenant has been found
if ($targetTenant -ne $null) {
    $tenantName = $targetTenant.Name
    $tenantId = $targetTenant.Id
    #Write-Output "Der Tenant mit dem Namen '$tenantName' hat die ID '$tenantId'."
} else {
    #Write-Output "Der Tenant mit dem Namen 'XXX' wurde nicht gefunden."
}

$TokenEndpoint = "https://login.microsoftonline.com/"+$tenantId+"/oauth2/v2.0/token"

$BodyBearer = @{
    grant_type="client_credentials"
    client_id="XXX..."
    client_secret="XXX..."
    scope="api://XXX.../.default"
}

$TokenResponse = Invoke-RestMethod -Uri $TokenEndpoint -Body $BodyBearer -Method Post 
$token = $TokenResponse.access_token

这正在工作,我得到了不记名令牌。

当我在通话中使用此令牌时会出现问题;返回的错误是:“未验证!”

我研究了各种身份验证方法,例如使用 auth_code (PKCE) 和 user_credentials 等。

我认为我必须分两步完成此操作。:

  1. 身份验证 (https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize)
  2. 创建承载 (https://login.microsoftonline.com/"+$tenantId+"/oauth2/v2.0/token)

首先我通过链接尝试使用 auth_code:

“https://login.microsoftonline.com/mytenant_id/oauth2/v2.0/authorize? 响应类型=代码& client_id=XXX& redirect_uri=http://localhost:5000/docs/oauth2-redirect& 范围=api://XXX/XXX& code_challenge=XXX& code_challenge_method=S256"

我们收到了代码:

0.ATsAD_kXDqOIk0-l18yEfP8wfhy2yu-....

我尝试使用此代码的方法:

$Body = @{
    # Telling Azure AD that we're using the auth code flow
    grant_type   = 'authorization_code'
    client_id="XXX..."
    client_secret="XXX..."
    scope="api://XXX.../.default"
    # The code from the browser from the previous step
    code         = '0.ATs....'
    code_verifier = "XXX...." #generated from code_challange
}
    try {
        $antwort = Invoke-RestMethod 'https://login.microsoftonline.com/tenant_id/oauth2/v2.0/token' -Method POST -Body $Body -ContenType 'application/x-www-form-urlencoded'
        
    } catch {
        $textBoxOutput = "Error during API request: $_"
    }

错误信息:

Error during API request: {"error":"invalid_client","error_description":"AADSTS700025: Client is public so neither 'client_assertion' nor 'client_secret' should be presented.

取出 client_secret.. 又收到一条错误消息:

Error during API request: {"error":"invalid_request","error_description":"AADSTS9002327: Tokens issued for the 'Single-Page Application' client-type may only be redeemed via cross-origin requests.

我一直在尝试解决该问题,但尚未找到解决方案。

万般无奈之下,我也尝试了不同的身份验证方法,但正如预期的那样,由于 Swagger 注释“auth method = Auth_code with PKCE”,没有任何结果。

我已经研究这个问题好几天了;我做错了什么? 有人可以帮我解决这个问题吗?

提前谢谢您。

powershell authentication oauth-2.0 bearer-token pkce
1个回答
0
投票

好的... 发现我的 API 在 azure 中配置为 SPA(单页应用程序)。 (错误信息...)

重新配置为“Web”,一切正常。

无论如何谢谢:D

© www.soinside.com 2019 - 2024. All rights reserved.