SYSLIB0028: X509Certificate2.PrivateKey is obsolete

The X509Certificate2.PrivateKey property is marked as obsolete, starting in .NET 6. Using this API in code generates warning SYSLIB0028 at compile time.

Workarounds

Use the appropriate method to get the private key, such as RSACertificateExtensions.GetRSAPrivateKey(X509Certificate2), or use the X509Certificate2.CopyWithPrivateKey(ECDiffieHellman) method to create a new instance with a private key.

Suppress a warning

If you must use the obsolete APIs, you can suppress the warning in code or in your project file.

To suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the warning.

// Disable the warning.
#pragma warning disable SYSLIB0028

// Code that uses obsolete API.
// ...

// Re-enable the warning.
#pragma warning restore SYSLIB0028

To suppress all the SYSLIB0028 warnings in your project, add a <NoWarn> property to your project file.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   ...
   <NoWarn>$(NoWarn);SYSLIB0028</NoWarn>
  </PropertyGroup>
</Project>

For more information, see Suppress warnings.