X509Certificate.Equals Method

Definition

Compares two X509Certificate objects for equality.

Overloads

Equals(Object)

Compares two X509Certificate objects for equality.

Equals(X509Certificate)

Compares two X509Certificate objects for equality.

Equals(Object)

Source:
X509Certificate.cs
Source:
X509Certificate.cs
Source:
X509Certificate.cs

Compares two X509Certificate objects for equality.

C#
public override bool Equals(object? obj);
C#
public override bool Equals(object obj);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public override bool Equals(object obj);

Parameters

obj
Object

An X509Certificate object to compare to the current object.

Returns

true if the current X509Certificate object is equal to the object specified by the other parameter; otherwise, false.

Attributes

Examples

The following example compares two certificates for equality.

C#

using System;
using System.Security.Cryptography.X509Certificates;

public class X509
{

    public static void Main()
    {

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

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

        // Load the certificate into an X509Certificate object.
        X509Certificate certTwo = X509Certificate.CreateFromCertFile(OtherCertificate);

        // Get the value.
        bool result = cert.Equals(certTwo);

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

Remarks

Two objects are considered equal if they are X509Certificate objects and they have the same issuer and serial number.

Important

The Equals method should not be used when comparing certificates for security purposes. Instead, use a hash of the RawData property, or the Thumbprint property.

Applies to

.NET 9 and other versions
Product Versions
.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
.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

Equals(X509Certificate)

Source:
X509Certificate.cs
Source:
X509Certificate.cs
Source:
X509Certificate.cs

Compares two X509Certificate objects for equality.

C#
public virtual bool Equals(System.Security.Cryptography.X509Certificates.X509Certificate? other);
C#
public virtual bool Equals(System.Security.Cryptography.X509Certificates.X509Certificate other);

Parameters

other
X509Certificate

An X509Certificate object to compare to the current object.

Returns

true if the current X509Certificate object is equal to the object specified by the other parameter; otherwise, false.

Examples

The following example compares two certificates for equality.

C#

using System;
using System.Security.Cryptography.X509Certificates;

public class X509
{

    public static void Main()
    {

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

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

        // Load the certificate into an X509Certificate object.
        X509Certificate certTwo = X509Certificate.CreateFromCertFile(OtherCertificate);

        // Get the value.
        bool result = cert.Equals(certTwo);

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

Remarks

Two X509Certificate objects are considered equal if they have the same issuer and serial number.

Important

The Equals method should not be used when comparing certificates for security purposes. Instead, use a hash of the RawData property, or the Thumbprint property.

Applies to

.NET 9 and other versions
Product Versions
.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
.NET Framework 1.1, 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