【问题标题】:AcquireTokenAsync gets Response status code does not indicate success: 401 (Unauthorized)AcquireTokenAsync 获取响应状态码不表示成功:401(未授权)
【发布时间】:2020-06-27 15:44:43
【问题描述】:

作为 ECR 的一部分,我正在更改代码以使用主题名称而不是我们的第一方 aad 应用程序的指纹来获取令牌。 我做了一些改变: 1.我在aad门户中的“主题名+发行人”中添加了主题名。 2.在AcquireTokenAsync函数调用中添加“sendX5c:true”参数。

我正在从我的机器上获取证书,但在尝试获取令牌时出现以下异常:AADSTS7000213:证书链无效。 “响应状态码不表示成功:401(未授权)”的内部异常。

知道我做错了什么或缺少什么吗?

我点击了这个链接:https://aadwiki.windows-int.net/index.php?title=Subject_Name_and_Issuer_Authentication

【问题讨论】:

  • 您好,Tsur,请参考我在下面提供的解决方案。如果有帮助,请accept它作为答案(单击我的答案旁边的复选标记,将其从灰色切换为已填充)。提前致谢。

标签: azure-active-directory certificate access-token


【解决方案1】:

如果您想通过客户端证书请求 Azure AD 访问令牌,请参考以下步骤

  1. 将证书信息上传到Azure

    一个。获取信息

     $Thumbprint="930CFA423637129DB45921320B0BB451BD58A813"
    $cert=Get-ChildItem -Path Cert:\LocalMachine\My\$Thumbprint
    $rawCert = $cert.GetRawCertData()
    $base64Cert = [System.Convert]::ToBase64String($rawCert)
    $rawCertHash = $cert.GetCertHash()
    $base64CertHash = [System.Convert]::ToBase64String($rawCertHash)
    $KeyId = [System.Guid]::NewGuid().ToString()
    

    b.将以上信息上传到 Azure

      Connect-AzureAD

    $clientAadApplication=Get-AzureADApplication -Filter "AppId eq '<you client id>'"

    New-AzureADApplicationKeyCredential -ObjectId $clientAadApplication.ObjectId  -CustomKeyIdentifier "$base64CertHash"  -Type AsymmetricX509Cert -Usage Verify -Value $base64Cert
  1. 获取访问令牌(我使用sdk Microsoft.Identity.Client
              String subjectname="";




                X509Store store = new X509Store(StoreName.My,StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection certCollection = store.Certificates;

                X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
                X509Certificate2Collection signingCert = currentCerts.Find(X509FindType.FindBySubjectName, subjectname, false);
                X509Certificate2 cer = signingCert.OfType<X509Certificate2>().OrderByDescending(c => c.NotBefore).FirstOrDefault();

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

更多详情请参考documentsample

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 1970-01-01
    • 2016-04-22
    • 2018-08-02
    • 1970-01-01
    • 2021-07-17
    • 2015-09-14
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多