【发布时间】:2018-09-26 17:31:34
【问题描述】:
我正在尝试构建要在 WPF 应用和 UWP 应用之间共享的组件。
我创建了一个 .NET Standard 库来包含公共代码,并且如果我将库版本保持为 1.2,则能够构建所有内容。
库需要从 X509Store 获取证书,因此:
internal X509Certificate2 GetClientCertificate(string thumbPrint)
{
if (string.IsNullOrEmpty(thumbPrint))
{
throw new ArgumentNullException(nameof(thumbPrint));
}
else
{
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, thumbPrint, true);
if (certificates.Count > 0)
{
return certificates[0];
}
else
{
throw new Exception($"No client certificate found for thumbprint {thumbPrint}");
}
}
}
此代码在放置在 UWP 或 WPF 项目中时有效,但在放置在 .NET Standard 库中时
- Visual Studio 指出我需要添加 System.dll 来解析 X509Certificate2 的定义(我这样做了,但这可以吗?),并且
- Visual Studio 指出我需要添加 mscorlib.dll 来解析一堆枚举,我尝试了但 VS 不会添加它。
.NET Standard 1.2 是否支持证书存储?如果没有,有没有其他方法(在两个项目中都引用相同的代码)我可以实现具有 X509Store 访问权限的组件?
【问题讨论】:
-
1.3 是最低要求:nuget.org/packages/…
-
@HansPassant 由于 WPF 应用程序,我降到了 1.2。原来它正在构建为不支持 1.3 的 4.5.1。将其升级到 4.6.1 允许它针对 .NET Standard 1.3 构建,所以现在一切看起来都很愉快。
标签: c# .net wpf uwp x509certificate2