简体   繁体   English

NET标准中的密码库

[英]Cryptography libraries in .NET Standard

I am trying to build a component to be shared between a WPF app and a UWP app. 我正在尝试构建一个在WPF应用程序和UWP应用程序之间共享的组件。

I have created a .NET Standard library to contain the common code, and am able to build everything if I keep the library version to 1.2. 我创建了一个.NET Standard库来包含通用代码,并且如果将库版本保持为1.2版,则能够构建所有内容。

The library needs to get a certificate from the X509Store thus: 该库需要从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}");
        }
    }
}

This code works if placed in either the UWP or WPF project, but when placed in the .NET Standard library 如果放置在UWP或WPF项目中,但是放置在.NET Standard库中,则此代码有效

  1. Visual Studio indicated that I needed to add System.dll to resolve the definition of X509Certificate2 (which I did, but is this OK?), and Visual Studio指示我需要添加System.dll来解决X509Certificate2的定义(我做了,但是可以吗?),并且
  2. Visual Studio indicated that I needed to add mscorlib.dll to resolve a bunch of enums, which I attempted but VS won't add it. Visual Studio指示我需要添加mscorlib.dll来解决一堆枚举,但我尝试这样做,但VS不会添加它。

Is the certificate store supported in .NET Standard 1.2? .NET Standard 1.2支持证书存储吗? If not, is there any other way (short of referencing the same code in both projects) I can implement my component having X509Store access? 如果没有,还有其他方法(在两个项目中都引用不到相同的代码)我可以实现具有X509Store访问权限的组件吗?

Your shared project can use multi-targeting feature of MSBuild to target, 您共享的项目可以使用MSBuild的多目标功能来定位,

  • .NET Standard .NET标准
  • .NET Framework 4.5.x .NET Framework 4.5.x

Then even without upgrading your WPF project to .NET Framework 4.6.1 you can share the code. 然后,即使不将WPF项目升级到.NET Framework 4.6.1,也可以共享代码。

You can find more tips in https://blog.lextudio.com/tips-for-net-nuget-package-authors-august-2017-48f07604e4a0 您可以在https://blog.lextudio.com/tips-for-net-nuget-package-authors-august-2017-48f07604e4a0中找到更多提示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM