'X509Certificate2' does not contain a definition for 'CopyWithPrivateKey'

Benjamin Salerno 6 Reputation points
2021-09-20T22:08:29.997+00:00

Hi, unfortunately I wasn't able to find any resources on this. I'm unable to properly access some of the Cryptography libraries in my project. My code looks like

.

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

            byte[] publicPemBytes = Encoding.UTF8.GetBytes(publicCert);
            using var publicX509 = new X509Certificate2(publicPemBytes);
            var privateKeyBlocks = privateCert.Split("-", 
            StringSplitOptions.RemoveEmptyEntries);
            var privateKeyBytes = Convert.FromBase64String(privateKeyBlocks[1]);

            RSA rsa = RSA.Create();
            if (privateKeyBlocks[0] == "BEGIN PRIVATE KEY")
            {
                rsa.ImportPkcs8PrivateKey(privateKeyBytes, out _);
            }
            else if (privateKeyBlocks[0] == "BEGIN RSA PRIVATE KEY")
            {
                rsa.ImportRSAPrivateKey(privateKeyBytes, out _);
            }
            X509Certificate2 keyPair = publicX509.CopyWithPrivateKey(rsa);
            return keyPair;

However I am getting a number of unexpected errors like "'X509Certificate2' does not contain a definition for 'CopyWithPrivateKey' and no accessible extension method 'CopyWithPrivateKey' accepting a first argument of type 'X509Certificate2' could be found" and "'RSA' does not contain a definition for 'ImportRSAPrivateKey' and no accessible extension method 'ImportRSAPrivateKey' accepting a first argument of type 'RSA' could be found" and "'RSA' does not contain a definition for 'ImportPkcs8PrivateKey' and no accessible extension method 'ImportPkcs8PrivateKey' accepting a first argument of type 'RSA' could be found "

I am not sure why these errors are occurring because I'm pretty sure I have all the correct imports. When I try to run this is a different project, I don't get these errors. Only when it's in the project I actually need it in. Is there a reason for this or something I'm doing wrong?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,300 questions
{count} votes

2 answers

Sort by: Most helpful
  1. P a u l 10,406 Reputation points
    2021-09-20T22:43:07.003+00:00
    1 person found this answer helpful.

  2. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2021-09-22T02:17:19.05+00:00

    @Benjamin Salerno , based on your description. I reproduced your problem. Pual's answer is correct, we need to upgrade the version to .net standard2.1.

    I noticed that you change the version in your .csproj file. I suggest that you could try to change the version in your properties, like the following picture:
    134165-image.png

    If it still exists the problem, I recommend that you create a new .netstandard 2.1 project to use your code.


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments