RemoteCertificate Property

SslStream.RemoteCertificate Property

Gets the certificate used to authenticate the remote endpoint.

Namespace:  System.Net.Security
Assembly:  System (in System.dll)

public virtual X509Certificate RemoteCertificate { get; }

Property Value

Type: System.Security.Cryptography.X509Certificates.X509Certificate
An X509Certificate object that represents the certificate supplied for authentication or null if no certificate was supplied.

ExceptionCondition
InvalidOperationException

Authentication failed or has not occurred.

The following code example demonstrates displaying the certificate returned by this property.

static void DisplayCertificateInformation(SslStream stream)
{
    Console.WriteLine("Certificate revocation list checked: {0}", stream.CheckCertRevocationStatus);

    X509Certificate localCertificate = stream.LocalCertificate;
    if (stream.LocalCertificate != null)
    {
        Console.WriteLine("Local cert was issued to {0} and is valid from {1} until {2}.",
            localCertificate.Subject,
            localCertificate.GetEffectiveDateString(),
            localCertificate.GetExpirationDateString());
     } else
    {
        Console.WriteLine("Local certificate is null.");
    }
    // Display the properties of the client's certificate.
    X509Certificate remoteCertificate = stream.RemoteCertificate;
    if (stream.RemoteCertificate != null)
    {
    Console.WriteLine("Remote cert was issued to {0} and is valid from {1} until {2}.",
        remoteCertificate.Subject,
        remoteCertificate.GetEffectiveDateString(),
        remoteCertificate.GetExpirationDateString());
    } else
    {
        Console.WriteLine("Remote certificate is null.");
    }
}
static void DisplayCertificateInformation(SslStream* stream)
{
    Console::WriteLine(S"Certificate revocation list checked: {0}", __box(stream->CheckCertRevocationStatus));

    X509Certificate* localCertificate = stream->LocalCertificate;
    if (stream->LocalCertificate != 0)
    {
        Console::WriteLine(S"Local cert was issued to {0} and is valid from {1} until {2}.",
            localCertificate->GetName(),
            localCertificate->GetEffectiveDateString(),
            localCertificate->GetExpirationDateString());
    } else
    {
        Console::WriteLine(S"Local certificate is null.");
    }
    // Display the properties of the client's certificate.
    X509Certificate* remoteCertificate = stream->RemoteCertificate;
    if (stream->RemoteCertificate != 0)
    {
        Console::WriteLine(S"Remote cert was issued to {0} and is valid from {1} until {2}.",
            remoteCertificate->GetName(),
            remoteCertificate->GetEffectiveDateString(),
            remoteCertificate->GetExpirationDateString());
    } else
    {
        Console::WriteLine(S"Remote certificate is null.");
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2016 Microsoft