首页 > 解决方案 > 访问 Azure Key Vault 时出现客户端认证异常

问题描述

这让我在过去的一天里一直很忙。

我正在尝试使用我的 .net core 2.2 应用程序中的 Azure Key Vaults 服务,并使用认证作为安全性。

我在应用程序启动时将证书安装在相同的代码中,使用 base64 编码的字符串作为输入参数。

StoreLocation.LocalMachine(并以管理员身份运行 VS)中安装时,应用程序可以正常工作并检索到机密。

使用时,应用程序在尝试从保管库中检索机密时StoreLocation.CurrentUser会抛出错误。NullReferenceException

   at Microsoft.IdentityModel.Clients.ActiveDirectory.CryptographyHelper.SignWithCertificate(String message, X509Certificate2 certificate)
   at Microsoft.IdentityModel.Clients.ActiveDirectory.JsonWebToken.Sign(IClientAssertionCertificate credential)
   at Microsoft.IdentityModel.Clients.ActiveDirectory.ClientKey.AddToParameters(IDictionary`2 parameters)
   at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.<SendTokenRequestAsync>d__64.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.<RunAsync>d__55.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext
....

早在 2017 年,当迁移到 .NET 4.7( HereHere)时,我就看到有人提到过同样的异常。

我尝试使用各种较新版本的Microsoft.IdentityModel.Clients.ActiveDirectory软件包运行该应用程序但没有成功。从 3.14 到 3.19 它抛出了同样的异常。从 4.3 开始,它缺少.Platforms软件包不再提供的 dll。

最后,我从Extensions 存储库中获取、编译并运行了示例应用程序(它也使用CurrentUser了 location 并且我有同样的异常。

我还没有尝试在 .NET Core 3.0 上运行它。但我认为最初的写作有问题。

我没有更多的资源来调查,所以我正在联系社区:)

下面是我用来编写/读取证书的简单示例应用程序的代码。

dotnet .\Certificates.dll -- -c {cert-base64-encoded-string} -p {cert-password}
public static Main(string[] args)
{
    //// Using CommandLineParser
    var options = Parser.Default.ParseArguments<Options>(args)
        .WithParsed(o =>
        {
            if (!string.IsNullOrEmpty(o.Certificate))
            {
                o.CertPassword = o.CertPassword ?? throw new ArgumentNullException("No certificate password provided.");

                var rawData = Convert.FromBase64String(o.Certificate);

                //// install certificate
                using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser, OpenFlags.ReadWrite))
                {
                    var cert = new X509Certificate2(rawData, o.CertPassword, X509KeyStorageFlags.PersistKeySet);
                    ThumbPrint = cert.Thumbprint;
                    store.Add(cert);
                }
            }
        });

    CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration(configHost =>
        {
            using (var store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
            {
                store.Open(OpenFlags.ReadOnly);

                var certs = store.Certificates.Find(X509FindType.FindByThumbprint, ThumbPrint, false);

                configHost.AddAzureKeyVault(_vault, _key, certs.OfType<X509Certificate2>().Single());

            }
        })
        .UseStartup<Startup>();

标签: azureasp.net-coreclient-certificatesazure-keyvault

解决方案


当我为这个问题的示例编写GitHub 自述文件时,我偶然发现了解决方案。

说这是人为错误让我很痛苦......

我为证书传递了错误的 Base64String。可能是我之前创建的。

在命令中传入正确生成的字符串时,代码开始工作。你可以在我的GitHub 上看到

简而言之,它失败了,因为我在客户端使用的证书与服务器上的证书不对应。尽管例外情况可能更清楚。


推荐阅读