【问题标题】:Did New-AzureADApplicationKeyCredential double base64 encode for CustomKeyIdentifier and Value?New-AzureADApplicationKeyCredential 是否对 CustomKeyIdentifier 和 Value 进行了双 base64 编码?
【发布时间】:2020-07-01 07:47:18
【问题描述】:

我正在使用 New-AzureADApplicationKeyCredential 为应用程序创建 KeyCredential。 document

首先,我对指纹进行base64编码

$cer = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cer.Import("C:\Users\PFuller\Desktop\abc.cer") 
...
$bin = $cer.GetCertHash()
$base64Thumbprint = [System.Convert]::ToBase64String($bin)
...
New-AzureADApplicationKeyCredential -ObjectId <id> 
                                    -CustomKeyIdentifier $base64Thumbprint  
                                    -Type AsymmetricX509Cert 
                                    -Usage Verify -Value $base64Value

但 AAD 清单上的结果是双 base64 编码

"keyCredentials": [
        {
            "customKeyIdentifier": "base64(base64Thumbprint)",
            ...
        }
    ],

根据Microsoft identity platform application authentication certificate credentials

customKeyIdentifier 应该只base64 编码一次并存储在清单的 keyCredentials 上。

我是不是误用了这个 cmd 或者这里出了什么问题?因为在我将清单转换为 base64 编码一次后,一切正常。

感谢您的帮助。

【问题讨论】:

  • 嗨,我的回答有用吗?如果您有任何更新,请告诉我:)

标签: c# azure powershell azure-active-directory


【解决方案1】:

根据我的测试,当我们使用 New-AzureADApplicationKeyCredential 为应用程序创建 KeyCredential 时,customKeyIdentifiervalue 都是双 base64 编码的。

之后,我使用以下代码通过此证书获取访问令牌:

var app = ConfidentialClientApplicationBuilder.Create("{clientId}")
               .WithAuthority(AzureCloudInstance.AzurePublic, "{tenantId}")
               .WithCertificate(cer)
               .Build();
var result = await app.AcquireTokenForClient(new[] { "https://graph.microsoft.com/.default" }).ExecuteAsync();
Console.WriteLine(result.AccessToken);

我发现我们可以通过正确的角色成功获取访问令牌。

所以我认为New-AzureADApplicationKeyCredential base64 会再次编码customKeyIdentifiervalue。但是当我们使用 X509Certificate 获取访问令牌时,它也能正确处理它们。

【讨论】:

  • 谢谢,抱歉回复晚了。在我的测试中,无论 customKeyIdentifier 是 base 64 编码一次还是两次。使用同一个 customKeyIdentifier 组成 JWT 都可以从 client_credentials 流中获取访问令牌。
猜你喜欢
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
  • 2020-10-12
  • 2011-02-27
  • 2023-03-20
  • 2013-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多