I have a crt and key file along with a passphrase.
I am using these successfully via Postman to call an external API. How do I do this in c#?
I see examples of using X509Certificate with httpclient but Idont see any options for a constructor whereI can use with 2 files and set the passphrase
.NET won't do this for you easily.
Your best bet, honestly, is to use something like OpenSSL to glue the cert and key together into a PFX.
The answer shall now continue assuming you decided not to do that.
There's no dearth of questions asking how to load a key without the certificate, e.g.:
Once you've figured out how to load the key you have a key and a certificate, and they don't understand each other. There are solutions.
If you are on .NET Core, or are using .NET Framework 4.7.2, you can use
X509Certificate2 certWithKey = cert.CopyWithPrivateKey(privateKey);
If you're adding certWithKey to an X509Store you either need to have used a persisted key, or export to PFX and import it back with X509KeyStorageFlags.PersistKeySet
If (all of):
RSACryptoServiceProvider
or a DSACryptoServiceProvider
!string.IsNullOrEmpty(key.CspKeyContainerInfo.KeyContainerName)
)then you could use the setter of X509Certificate2.PrivateKey.
There are some P/Invoke recommendations in the bottom of my answer to .NET Standard - Merge a certificate and a private key into a .pfx file programmatically for making a PFX. Once you've made a PFX you are back in "simple" territory.