.Net6 has a function X509Certificate2.CreateFromPem. What is the way to use client cert and key.pem file and create X509Certificate2 object in .Net Standard 2.1.
.NET Standard 2.1 is an unusual target, as it is essentially just the .NET Core development line (although Unity has updated their copy of .NET to be .NET Standard 2.1-compatible). So the most common industry answer would be to avoid that target and just target a .NET Core / .NET 5+ version that meets your needs.
If you do want to target .NET Standard 2.1, though, you need to
byte[]
(encoded contents) or string
(filename) constructor, either of which understand the PEM format (but won't load the private key).
Encoding.Utf8.GetBytes
cert.GetKeyAlgorithm()
to understand what algorithm the private key uses. Call RSA.Create(), ECDsa.Create(), ECDiffieHellman.Create(), or DSA.Create() as appropriate.X509Certificate2 combined = cert.CopyWithPrivateKey(key);
https://github.com/dotnet/runtime/blob/9e8d0a81a35f05eaa2c4d0ab258ed9a1f4e2ec76/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs#L937-L963 may be a reasonable starting point... but a lot of its implementation uses things not available in .NET Standard 2.1.