Error (active) CS1061 'RSA' does not contain a definition for 'ImportPkcs8PrivateKey' and no accessible extension method 'ImportPkcs8PrivateKey' accepting a first argument of type 'RSA' could be found (are you missing a using directive or an assembly refe

try again 20 Reputation points
2024-09-28T13:12:38.52+00:00

I am using Visual Studio 2022 and have set up a console application.

It tells me that mportPkcs8PrivateKey is not implemented.

This method is indeed implemented with dotnet core 8

https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsa.importpkcs8privatekey?view=net-8.0

What am I doing wrong? Is there any missing nuget package?

Thanks

using System.Security.Cryptography;

privateKey = privateKey.Replace("-----BEGIN PRIVATE KEY-----", string.Empty).Replace("-----END PRIVATE KEY-----", string.Empty);

privateKey = privateKey.Replace(Environment.NewLine, string.Empty);

// CHANGE CONVERSION TYPE

var privateKeyBytes = Convert.FromBase64String(privateKey);

var rsa = RSA.Create();

rsa.ImportPkcs8PrivateKey(privateKeyBytes, out _);

Severity Code Description Project File Line Suppression State

Error (active) CS1061 'RSA' does not contain a definition for 'ImportPkcs8PrivateKey' and no accessible extension method 'ImportPkcs8PrivateKey' accepting a first argument of type 'RSA' could be found (are you missing a using directive or an assembly reference?) ConsoleApp4 - jwt C:\Users\hp800\source\repos\ConsoleApp4 - jwt\ConsoleApp4 - jwt\Program.cs 67

framework dotnet 4.8.1

core dotnet 8.0.304

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,945 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 65,556 Reputation points
    2024-09-28T15:37:12.84+00:00

    C# is strongly typed language. There is no signature of ImportPkcs8PrivateKey() that accepts a byte array, you must convert privateKeyBytes to a ReadOnlySpan<byte>.


1 additional answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 45,506 Reputation points Microsoft Vendor
    2024-09-30T06:58:59.0666667+00:00

    Hi @try again , Welcome to Microsoft Q&A,

    RSA.ImportPkcs8PrivateKey(ReadOnlySpan<Byte>, Int32) Method only supports Core 3.0, Core 3.1, 5, 6, 7, 8, 9.

    In my test, you may have created the wrong platform. In the .Net 8.0 Console project I created, it can be recognized correctly. If it is a .Net Framework 4.8..1 project, the above error will appear. This is caused by the wrong platform.

    User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    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.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.