X509Certificate.Import Method

Definition

Populates an X509Certificate object.

Overloads

Import(String, String, X509KeyStorageFlags)
Obsolete.
Obsolete.

Populates the X509Certificate object with information from a certificate file, a password, and a X509KeyStorageFlags value.

Import(String, SecureString, X509KeyStorageFlags)
Obsolete.
Obsolete.

Populates an X509Certificate object with information from a certificate file, a password, and a key storage flag.

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

Populates the X509Certificate object using data from a byte array, a password, and flags for determining how the private key is imported.

Import(Byte[])
Obsolete.
Obsolete.

Populates the X509Certificate object with data from a byte array.

Import(String)
Obsolete.
Obsolete.

Populates the X509Certificate object with information from a certificate file.

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

Populates an X509Certificate object using data from a byte array, a password, and a key storage flag.

Import(String, String, X509KeyStorageFlags)

Source:
X509Certificate.cs
Source:
X509Certificate.cs
Source:
X509Certificate.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.

Populates the X509Certificate object with information from a certificate file, a password, and a X509KeyStorageFlags value.

C#
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use X509CertificateLoader to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public virtual void Import(string fileName, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
public virtual void Import(string fileName, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
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 virtual void Import(string fileName, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
public virtual void Import(string fileName, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public virtual void Import(string fileName, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public virtual void Import(string fileName, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);

Parameters

fileName
String

The name of a certificate file represented as a string.

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

The fileName parameter is null.

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

Remarks

Important

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 the assembly in a text editor such as Notepad.exe.

Applies to

.NET 10 and other versions
Product Versions (Obsolete)
.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

Import(String, SecureString, X509KeyStorageFlags)

Source:
X509Certificate.cs
Source:
X509Certificate.cs
Source:
X509Certificate.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.

Important

This API is not CLS-compliant.

Populates an X509Certificate object with information from a certificate file, a password, and a key storage flag.

C#
[System.CLSCompliant(false)]
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use X509CertificateLoader to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public virtual void Import(string fileName, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.CLSCompliant(false)]
public virtual void Import(string fileName, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.CLSCompliant(false)]
[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 virtual void Import(string fileName, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.CLSCompliant(false)]
public virtual void Import(string fileName, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
public virtual void Import(string fileName, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.Security.SecurityCritical]
public virtual void Import(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

The fileName parameter is null.

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

Remarks

Important

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 the assembly in a text editor such as Notepad.exe.

Applies to

.NET 10 and other versions
Product Versions (Obsolete)
.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

Import(Byte[], String, X509KeyStorageFlags)

Source:
X509Certificate.cs
Source:
X509Certificate.cs
Source:
X509Certificate.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.

Populates the X509Certificate object using data from a byte array, a password, and flags for determining how the private key is imported.

C#
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use X509CertificateLoader to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public virtual void Import(byte[] rawData, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
public virtual void Import(byte[] rawData, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
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 virtual void Import(byte[] rawData, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
public virtual void Import(byte[] rawData, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public virtual void Import(byte[] rawData, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public virtual void Import(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

The rawData parameter is null.

-or-

The length of the rawData parameter is 0.

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

Remarks

This method can be used to populate an X509Certificate object using a password for the certificate represented by the byte array. The X509KeyStorageFlags value can be used to control where and how the private key is imported.

Applies to

.NET 10 and other versions
Product Versions (Obsolete)
.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

Import(Byte[])

Source:
X509Certificate.cs
Source:
X509Certificate.cs
Source:
X509Certificate.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.

Populates the X509Certificate object with data from a byte array.

C#
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use X509CertificateLoader to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public virtual void Import(byte[] rawData);
C#
public virtual void Import(byte[] rawData);
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 virtual void Import(byte[] rawData);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public virtual void Import(byte[] rawData);
C#
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public virtual void Import(byte[] rawData);

Parameters

rawData
Byte[]

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

Attributes

Exceptions

The rawData parameter is null.

-or-

The length of the rawData parameter is 0.

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

Remarks

This method can be used to take the raw byte array of an X.509 certificate and populate the X509Certificate object with its associated values.

Applies to

.NET 10 and other versions
Product Versions (Obsolete)
.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

Import(String)

Source:
X509Certificate.cs
Source:
X509Certificate.cs
Source:
X509Certificate.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.

Populates the X509Certificate object with information from a certificate file.

C#
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use X509CertificateLoader to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public virtual void Import(string fileName);
C#
public virtual void Import(string fileName);
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 virtual void Import(string fileName);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public virtual void Import(string fileName);
C#
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public virtual void Import(string fileName);

Parameters

fileName
String

The name of a certificate file represented as a string.

Attributes

Exceptions

The fileName parameter is null.

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

Examples

The following example loads an X.509 certificate from a file, calls the ToString method, and displays the results to the console.

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

class X509
{

    static void Main()
    {

        // The path to the certificate.
        string Certificate = "Certificate.cer";

        // Load the certificate into an X509Certificate object.
        X509Certificate cert = new X509Certificate();

        cert.Import(Certificate);

        // Get the value.
        string resultsTrue = cert.ToString(true);

        // Display the value to the console.
        Console.WriteLine(resultsTrue);

        // Get the value.
        string resultsFalse = cert.ToString(false);

        // Display the value to the console.
        Console.WriteLine(resultsFalse);
    }
}

Remarks

This method uses a certificate file (such as a file with a .cer extension) that represents an X.509 certificate, and populates the X509Certificate object with the certificate that the file contains.

Applies to

.NET 10 and other versions
Product Versions (Obsolete)
.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

Import(Byte[], SecureString, X509KeyStorageFlags)

Source:
X509Certificate.cs
Source:
X509Certificate.cs
Source:
X509Certificate.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.

Important

This API is not CLS-compliant.

Populates an X509Certificate object using data from a byte array, a password, and a key storage flag.

C#
[System.CLSCompliant(false)]
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use X509CertificateLoader to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public virtual void Import(byte[] rawData, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.CLSCompliant(false)]
public virtual void Import(byte[] rawData, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.CLSCompliant(false)]
[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 virtual void Import(byte[] rawData, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.CLSCompliant(false)]
public virtual void Import(byte[] rawData, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
public virtual void Import(byte[] rawData, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
C#
[System.Security.SecurityCritical]
public virtual void Import(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

The rawData parameter is null.

-or-

The length of the rawData parameter is 0.

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

Remarks

Important

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 the assembly in a text editor such as Notepad.exe.

Applies to

.NET 10 and other versions
Product Versions (Obsolete)
.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