question

BenjaminSalerno-8067 avatar image
0 Votes"
BenjaminSalerno-8067 asked JackJJun-MSFT commented

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

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?

dotnet-csharpdotnet-macos
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is the project you're trying to use this in a .netstandard project?

0 Votes 0 ·

Yes. Target framework is netstandard2.0

0 Votes 0 ·
JackJJun-MSFT avatar image JackJJun-MSFT BenjaminSalerno-8067 ·

@BenjaminSalerno-8067 , I have not heard from you for a couple of days. Please let me know if there is anything that I can help in here.

0 Votes 0 ·
Paul-5034 avatar image
1 Vote"
Paul-5034 answered BenjaminSalerno-8067 edited
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is there a quick and simple way to upgrade from 2.0 to 2.1?

0 Votes 0 ·
Paul-5034 avatar image Paul-5034 BenjaminSalerno-8067 ·

It should just be a case of changing the runtime version from netstandard2.0 to netstandard2.1 in your project file.

0 Votes 0 ·

I changed the version from netstandard2.0 to netstandard2.1 in my .csproj file. However, when I try to build it tells me "project is not compatible with netcoreapp2.1"

0 Votes 0 ·
Show more comments
JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered JackJJun-MSFT edited

@BenjaminSalerno-8067, 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.



image.png (17.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.