azure - 访问 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( Here和Here)时,我就看到有人提到过同样的异常。
我尝试使用各种较新版本的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>();
解决方案
当我为这个问题的示例编写GitHub 自述文件时,我偶然发现了解决方案。
说这是人为错误让我很痛苦......
我为证书传递了错误的 Base64String。可能是我之前创建的。
在命令中传入正确生成的字符串时,代码开始工作。你可以在我的GitHub 上看到
简而言之,它失败了,因为我在客户端使用的证书与服务器上的证书不对应。尽管例外情况可能更清楚。
推荐阅读
- python - 在子类 QtCore.QAbstractItemModel 上使用 QSortFilterProxyModel 时,PyQt5 Treeview 不需要扩展索引
- java - java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton 不能转换为 android.widget.Button
- react-native - React Native“显示检查器”重新加载应用程序
- java - Java中的对象在内存中的字段和值遍历
- javascript - 传递 ID 以在 vue 中为特定项目打开模式的问题
- mysql - MySQL 从带有两个关系表的选择查询中提取 JSON
- android - 从图像而不是来自实时相机的张量流对象检测
- javascript - 如何正确索引 Javascript 中的嵌套数组?
- javascript - 循环遍历数组以过滤对象数组
- c# - 由于日期实现,C# 从 swagger 生成的客户端无法正常工作