简体   繁体   中英

Cryptography libraries in .NET Standard

I am trying to build a component to be shared between a WPF app and a UWP app.

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.

The library needs to get a certificate from the X509Store thus:

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

  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
  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.

Is the certificate store supported in .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?

Your shared project can use multi-targeting feature of MSBuild to target,

  • .NET Standard
  • .NET Framework 4.5.x

Then even without upgrading your WPF project to .NET Framework 4.6.1 you can share the code.

You can find more tips in https://blog.lextudio.com/tips-for-net-nuget-package-authors-august-2017-48f07604e4a0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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