Sample code for 30+ languages & platforms
C#

Get the Server Certificate from a TLS Connection

See more Socket/SSL/TLS Examples

Demonstrates Socket.GetServerCert, which copies the certificate presented by the remote TLS server into a Cert object for inspection.

Background. The method returns false when the socket is not connected, is not using TLS, or no server certificate is available. Inspecting the certificate lets an application validate the server's identity.

Chilkat C# Downloads

C#
bool success = false;

Chilkat.Socket socket = new Chilkat.Socket();

//  Connect to the server using TLS.
bool bTls = true;
int maxWaitMs = 5000;
success = socket.Connect("example.com",5000,bTls,maxWaitMs);
if (success == false) {
    Debug.WriteLine(socket.LastErrorText);
    return;
}

//  Copy the certificate presented by the remote TLS server, then inspect it.  This can be used to
//  validate the server's identity.
Chilkat.Cert cert = new Chilkat.Cert();
success = socket.GetServerCert(cert);
if (success == false) {
    Debug.WriteLine(socket.LastErrorText);
    return;
}

Debug.WriteLine("Server certificate subject: " + cert.SubjectCN);
Debug.WriteLine("Issued by: " + cert.IssuerCN);