X509Certificate2 Constructors

Definition

Initializes a new instance of the X509Certificate2 class.

Overloads

X509Certificate2()
Obsolete.
Obsolete.

Initializes a new instance of the X509Certificate2 class.

X509Certificate2(String, String, X509KeyStorageFlags)
Obsolete.

Initializes a new instance of the X509Certificate2 class using a certificate file name, a password used to access the certificate, and a key storage flag.

X509Certificate2(String, SecureString, X509KeyStorageFlags)
Obsolete.

Initializes a new instance of the X509Certificate2 class using a certificate file name, a password, and a key storage flag.

X509Certificate2(String, ReadOnlySpan<Char>, X509KeyStorageFlags)
Obsolete.

Initializes a new instance of the X509Certificate2 class using a certificate file name, a password, and a key storage flag.

X509Certificate2(Byte[], String, X509KeyStorageFlags)
Obsolete.

Initializes a new instance of the X509Certificate2 class using a byte array, a password, and a key storage flag.

X509Certificate2(Byte[], SecureString, X509KeyStorageFlags)
Obsolete.

Initializes a new instance of the X509Certificate2 class using a byte array, a password, and a key storage flag.

X509Certificate2(String, String)
Obsolete.

Initializes a new instance of the X509Certificate2 class using a certificate file name and a password used to access the certificate.

X509Certificate2(String, SecureString)
Obsolete.

Initializes a new instance of the X509Certificate2 class using a certificate file name and a password.

X509Certificate2(ReadOnlySpan<Byte>, ReadOnlySpan<Char>, X509KeyStorageFlags)
Obsolete.

Initializes a new instance of the X509Certificate2 class from certificate data, a password, and key storage flags.

X509Certificate2(Byte[], String)
Obsolete.

Initializes a new instance of the X509Certificate2 class using a byte array and a password.

X509Certificate2(Byte[], SecureString)
Obsolete.

Initializes a new instance of the X509Certificate2 class using a byte array and a password.

X509Certificate2(String)
Obsolete.

Initializes a new instance of the X509Certificate2 class using a certificate file name.

X509Certificate2(X509Certificate)

Initializes a new instance of the X509Certificate2 class using an X509Certificate object.

X509Certificate2(SerializationInfo, StreamingContext)
Obsolete.

Initializes a new instance of the X509Certificate2 class using the specified serialization and stream context information.

X509Certificate2(ReadOnlySpan<Byte>)
Obsolete.

Initializes a new instance of the X509Certificate2 class from certificate data.

X509Certificate2(IntPtr)

Initializes a new instance of the X509Certificate2 class using an unmanaged handle.

X509Certificate2(Byte[])
Obsolete.

Initializes a new instance of the X509Certificate2 class using information from a byte array.

X509Certificate2()

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

X509Certificate and X509Certificate2 are immutable. Use X509CertificateLoader to create a new certificate.

Caution

X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.

Initializes a new instance of the X509Certificate2 class.

C#
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use X509CertificateLoader to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2();
C#
public X509Certificate2();
C#
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public X509Certificate2();
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public X509Certificate2();
Attributes

Examples

The following code example opens the current user certificate store, selects only active certificates, then allows the user to select one or more certificates. The example then writes certificate information to the console.

C#
using System;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.IO;
using System.Security.Cryptography.X509Certificates;

class CertSelect
{
    static void Main()
    {
        X509Store store = new X509Store("MY",StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

        X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
        X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid,DateTime.Now,false);
        X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Test Certificate Select","Select a certificate from the following list to get information on that certificate",X509SelectionFlag.MultiSelection);
        Console.WriteLine("Number of certificates: {0}{1}",scollection.Count,Environment.NewLine);

        foreach (X509Certificate2 x509 in scollection)
        {
            try
            {
                byte[] rawdata = x509.RawData;
                Console.WriteLine("Content Type: {0}{1}",X509Certificate2.GetCertContentType(rawdata),Environment.NewLine);
                Console.WriteLine("Friendly Name: {0}{1}",x509.FriendlyName,Environment.NewLine);
                Console.WriteLine("Certificate Verified?: {0}{1}",x509.Verify(),Environment.NewLine);
                Console.WriteLine("Simple Name: {0}{1}",x509.GetNameInfo(X509NameType.SimpleName,true),Environment.NewLine);
                Console.WriteLine("Signature Algorithm: {0}{1}",x509.SignatureAlgorithm.FriendlyName,Environment.NewLine);
                Console.WriteLine("Public Key: {0}{1}",x509.PublicKey.Key.ToXmlString(false),Environment.NewLine);
                Console.WriteLine("Certificate Archived?: {0}{1}",x509.Archived,Environment.NewLine);
                Console.WriteLine("Length of Raw Data: {0}{1}",x509.RawData.Length,Environment.NewLine);
                X509Certificate2UI.DisplayCertificate(x509);
                x509.Reset();
            }
            catch (CryptographicException)
            {
                Console.WriteLine("Information could not be written out for this certificate.");
            }
        }
        store.Close();
    }
}

Remarks

This constructor creates an empty X509Certificate2 object, unlike the other constructors for this class that use certificate information from a byte array, a pointer, or a certificate file.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5 (6, 7, 8, 9, 10)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

X509Certificate2(String, String, X509KeyStorageFlags)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Initializes a new instance of the X509Certificate2 class using a certificate file name, a password used to access the certificate, and a key storage flag.

C#
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(string fileName, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
public X509Certificate2(string fileName, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(string fileName, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
public X509Certificate2(string fileName, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);

Parameters

fileName
String

The name of a certificate file.

password
String

The password required to access the X.509 certificate data.

keyStorageFlags
X509KeyStorageFlags

A bitwise combination of the enumeration values that control where and how to import the certificate.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

This constructor creates a new X509Certificate2 object using a certificate file name, a password needed to access the certificate, and a key storage flag.

Dôležité

Never hard code a password within your source code. Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

If you create an X509Certificate2 certificate by specifying a PKCS7 signed file store for fileName, the X509Certificate2 is created for the certificate that signed the store rather than for any of the certificates within the store.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (9, 10)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

X509Certificate2(String, SecureString, X509KeyStorageFlags)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Important

This API is not CLS-compliant.

Initializes a new instance of the X509Certificate2 class using a certificate file name, a password, and a key storage flag.

C#
[System.CLSCompliant(false)]
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(string fileName, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.CLSCompliant(false)]
public X509Certificate2(string fileName, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.CLSCompliant(false)]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(string fileName, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.CLSCompliant(false)]
public X509Certificate2(string fileName, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
public X509Certificate2(string fileName, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);

Parameters

fileName
String

The name of a certificate file.

password
SecureString

The password required to access the X.509 certificate data.

keyStorageFlags
X509KeyStorageFlags

A bitwise combination of the enumeration values that control where and how to import the certificate.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

If you create an X509Certificate certificate by specifying a PKCS7 signed file store for fileName, the X509Certificate is created for the certificate that signed the store rather than for any of the certificates within the store.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (9, 10)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

X509Certificate2(String, ReadOnlySpan<Char>, X509KeyStorageFlags)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Initializes a new instance of the X509Certificate2 class using a certificate file name, a password, and a key storage flag.

C#
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(string fileName, ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags = System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.DefaultKeySet);
C#
public X509Certificate2(string fileName, ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags = System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.DefaultKeySet);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(string fileName, ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags = System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.DefaultKeySet);

Parameters

fileName
String

The name of a certificate file.

password
ReadOnlySpan<Char>

The password required to access the X.509 certificate data.

keyStorageFlags
X509KeyStorageFlags

A bitwise combination of the enumeration values that control where and how to import the certificate.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

Dôležité

Never hard code a password within your source code. Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

If you create an X509Certificate2 certificate by specifying a PKCS7 signed file store for fileName, the X509Certificate2 is created for the certificate that signed the store rather than for any of the certificates within the store.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET 5, 6, 7, 8 (9, 10)

X509Certificate2(Byte[], String, X509KeyStorageFlags)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Initializes a new instance of the X509Certificate2 class using a byte array, a password, and a key storage flag.

C#
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(byte[] rawData, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
public X509Certificate2(byte[] rawData, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(byte[] rawData, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
public X509Certificate2(byte[] rawData, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);

Parameters

rawData
Byte[]

A byte array containing data from an X.509 certificate.

password
String

The password required to access the X.509 certificate data.

keyStorageFlags
X509KeyStorageFlags

A bitwise combination of the enumeration values that control where and how to import the certificate.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

This constructor creates a new X509Certificate2 object using a byte array, a password that is needed to access the certificate data, and a key storage flag. It is used with PKCS12 (PFX) files that contain the certificate's private key. Calling this constructor with the correct password decrypts the private key and saves it to a Microsoft Cryptographic API Cryptographic Service Provider (CSP).

Dôležité

Never hard code a password within your source code. Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

If you create an X509Certificate2 certificate by specifying a PKCS7 signed file store for rawData, the X509Certificate2 is created for the certificate that signed the store rather than for any of the certificates within the store.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (9, 10)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

X509Certificate2(Byte[], SecureString, X509KeyStorageFlags)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Important

This API is not CLS-compliant.

Initializes a new instance of the X509Certificate2 class using a byte array, a password, and a key storage flag.

C#
[System.CLSCompliant(false)]
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(byte[] rawData, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.CLSCompliant(false)]
public X509Certificate2(byte[] rawData, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.CLSCompliant(false)]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(byte[] rawData, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.CLSCompliant(false)]
public X509Certificate2(byte[] rawData, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
public X509Certificate2(byte[] rawData, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);

Parameters

rawData
Byte[]

A byte array that contains data from an X.509 certificate.

password
SecureString

The password required to access the X.509 certificate data.

keyStorageFlags
X509KeyStorageFlags

A bitwise combination of the enumeration values that control where and how to import the certificate.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

This constructor is used with PKCS12 (PFX) files that contain the certificate's private key. Calling this constructor with the correct password decrypts the private key and saves it to a Microsoft Cryptographic API Cryptographic Service Provider (CSP).

Dôležité

Never hard code a password within your source code. Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

If you create an X509Certificate certificate by specifying a PKCS7 signed file store for rawData, the X509Certificate is created for the certificate that signed the store rather than for any of the certificates within the store.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (9, 10)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

X509Certificate2(String, String)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Initializes a new instance of the X509Certificate2 class using a certificate file name and a password used to access the certificate.

C#
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(string fileName, string? password);
C#
public X509Certificate2(string fileName, string? password);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(string fileName, string? password);
C#
public X509Certificate2(string fileName, string password);

Parameters

fileName
String

The name of a certificate file.

password
String

The password required to access the X.509 certificate data.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

This constructor creates a new X509Certificate2 object using a certificate file name and a password needed to access the certificate. It is used with PKCS12 (PFX) files that contain the certificate's private key. Calling this constructor with the correct password decrypts the private key and saves it to a key container.

Dôležité

Never hard code a password within your source code. Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

If you create an X509Certificate2 certificate by specifying a PKCS7 signed file store for fileName, the X509Certificate2 is created for the certificate that signed the store rather than for any of the certificates within the store.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (9, 10)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

X509Certificate2(String, SecureString)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Important

This API is not CLS-compliant.

Initializes a new instance of the X509Certificate2 class using a certificate file name and a password.

C#
[System.CLSCompliant(false)]
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(string fileName, System.Security.SecureString? password);
C#
[System.CLSCompliant(false)]
public X509Certificate2(string fileName, System.Security.SecureString? password);
C#
[System.CLSCompliant(false)]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(string fileName, System.Security.SecureString? password);
C#
[System.CLSCompliant(false)]
public X509Certificate2(string fileName, System.Security.SecureString password);
C#
public X509Certificate2(string fileName, System.Security.SecureString password);

Parameters

fileName
String

The name of a certificate file.

password
SecureString

The password required to access the X.509 certificate data.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

This constructor is used with PKCS12 (PFX) files that contain the certificate's private key. Calling this constructor with the correct password decrypts the private key and saves it to a key container.

Dôležité

Never hard code a password within your source code. Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

If you create an X509Certificate certificate by specifying a PKCS7 signed file store for fileName, the X509Certificate is created for the certificate that signed the store rather than for any of the certificates within the store.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (9, 10)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

X509Certificate2(ReadOnlySpan<Byte>, ReadOnlySpan<Char>, X509KeyStorageFlags)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Initializes a new instance of the X509Certificate2 class from certificate data, a password, and key storage flags.

C#
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(ReadOnlySpan<byte> rawData, ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags = System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.DefaultKeySet);
C#
public X509Certificate2(ReadOnlySpan<byte> rawData, ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags = System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.DefaultKeySet);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(ReadOnlySpan<byte> rawData, ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags = System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.DefaultKeySet);

Parameters

rawData
ReadOnlySpan<Byte>

The certificate data to process.

password
ReadOnlySpan<Char>

The password required to access the certificate data.

keyStorageFlags
X509KeyStorageFlags

A bitwise combination of the enumeration values that control where and how to import the certificate.

Attributes

Exceptions

An error with the certificate occurs.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET 5, 6, 7, 8 (9, 10)

X509Certificate2(Byte[], String)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Initializes a new instance of the X509Certificate2 class using a byte array and a password.

C#
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(byte[] rawData, string? password);
C#
public X509Certificate2(byte[] rawData, string? password);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(byte[] rawData, string? password);
C#
public X509Certificate2(byte[] rawData, string password);

Parameters

rawData
Byte[]

A byte array containing data from an X.509 certificate.

password
String

The password required to access the X.509 certificate data.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

This constructor creates a new X509Certificate2 object using a byte array and a password that is needed to access the certificate data. It is used with PKCS12 (PFX) files that contain the certificate's private key. Calling this constructor with the correct password decrypts the private key and saves it to a key container.

Dôležité

Never hard code a password within your source code. Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

If you create an X509Certificate2 certificate by specifying a PKCS7 signed file store for rawData, the X509Certificate2 is created for the certificate that signed the store rather than for any of the certificates within the store.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (9, 10)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

X509Certificate2(Byte[], SecureString)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Important

This API is not CLS-compliant.

Initializes a new instance of the X509Certificate2 class using a byte array and a password.

C#
[System.CLSCompliant(false)]
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(byte[] rawData, System.Security.SecureString? password);
C#
[System.CLSCompliant(false)]
public X509Certificate2(byte[] rawData, System.Security.SecureString? password);
C#
[System.CLSCompliant(false)]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(byte[] rawData, System.Security.SecureString? password);
C#
[System.CLSCompliant(false)]
public X509Certificate2(byte[] rawData, System.Security.SecureString password);
C#
public X509Certificate2(byte[] rawData, System.Security.SecureString password);

Parameters

rawData
Byte[]

A byte array that contains data from an X.509 certificate.

password
SecureString

The password required to access the X.509 certificate data.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

This constructor is used with PKCS12 (PFX) files that contain the certificate's private key. Calling this constructor with the correct password decrypts the private key and saves it to a key container.

Dôležité

Never hard code a password within your source code. Hard-coded passwords can be retrieved from an assembly using the Ildasm.exe (IL Disassembler), a hex editor, or by simply opening up the assembly in a text editor such as Notepad.exe.

If you create an X509Certificate certificate by specifying a PKCS7 signed file store for rawData, the X509Certificate is created for the certificate that signed the store rather than for any of the certificates within the store.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (9, 10)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

X509Certificate2(String)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Initializes a new instance of the X509Certificate2 class using a certificate file name.

C#
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(string fileName);
C#
public X509Certificate2(string fileName);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(string fileName);

Parameters

fileName
String

The name of a certificate file.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

This constructor creates a new X509Certificate2 object using a certificate file name. It supports binary (DER) encoding or PEM encoding.

If you create an X509Certificate2 certificate by specifying a PKCS7 signed file store for fileName, the X509Certificate2 is created for the certificate that signed the store rather than for any of the certificates within the store.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (9, 10)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

X509Certificate2(X509Certificate)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Initializes a new instance of the X509Certificate2 class using an X509Certificate object.

C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(System.Security.Cryptography.X509Certificates.X509Certificate certificate);
C#
public X509Certificate2(System.Security.Cryptography.X509Certificates.X509Certificate certificate);

Parameters

certificate
X509Certificate

An X509Certificate object.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

This method creates a new instance of the X509Certificate2 class using an X509Certificate object.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

X509Certificate2(SerializationInfo, StreamingContext)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

Initializes a new instance of the X509Certificate2 class using the specified serialization and stream context information.

C#
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected X509Certificate2(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
C#
protected X509Certificate2(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);

Parameters

info
SerializationInfo

The serialization information required to deserialize the new X509Certificate2.

context
StreamingContext

Contextual information about the source of the stream to be deserialized.

Attributes

Exceptions

.NET Core and .NET 5+ only: In all cases.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7 (8, 9, 10)
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

X509Certificate2(ReadOnlySpan<Byte>)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Initializes a new instance of the X509Certificate2 class from certificate data.

C#
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(ReadOnlySpan<byte> rawData);
C#
public X509Certificate2(ReadOnlySpan<byte> rawData);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(ReadOnlySpan<byte> rawData);

Parameters

rawData
ReadOnlySpan<Byte>

The certificate data to process.

Attributes

Exceptions

An error with the certificate occurs.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET 5, 6, 7, 8 (9, 10)

X509Certificate2(IntPtr)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Initializes a new instance of the X509Certificate2 class using an unmanaged handle.

C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(IntPtr handle);
C#
public X509Certificate2(IntPtr handle);

Parameters

handle
IntPtr

A pointer to a certificate context in unmanaged code. The C structure is called PCCERT_CONTEXT.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

This constructor creates a new X509Certificate2 object using a handle for the Microsoft Cryptographic API certificate context, PCCERT_CONTEXT. Note that the immediate caller of this constructor requires unmanaged code permission.

Dôležité

The constructor creates a copy of the certificate context. Do not assume that the context structure you passed to the constructor is valid; it may have been released. You can get a copy of the current PCCERT_CONTEXT structure from the Handle property, but it is valid only during the lifetime of the X509Certificate2 object.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

X509Certificate2(Byte[])

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

Caution

Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.

Initializes a new instance of the X509Certificate2 class using information from a byte array.

C#
[System.Obsolete("Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.", DiagnosticId="SYSLIB0057", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(byte[] rawData);
C#
public X509Certificate2(byte[] rawData);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public X509Certificate2(byte[] rawData);

Parameters

rawData
Byte[]

A byte array containing data from an X.509 certificate.

Attributes

Exceptions

An error with the certificate occurs. For example:

  • The certificate file does not exist.

  • The certificate is invalid.

  • The certificate's password is incorrect.

Remarks

This constructor creates a new X509Certificate2 object using certificate information from a byte array. The byte array can be binary (DER) encoded or PEM-encoded X.509 data. The byte array can also be a PKCS7 (Authenticode) signed file; the signer certificate is used to create the object.

If you create an X509Certificate2 certificate by specifying a PKCS7 signed file store for rawData, the X509Certificate2 is created for the certificate that signed the store rather than for any of the certificates within the store.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie (Zastarané)
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (9, 10)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1