Reading SSL Certificate Details In C#

What Is SSL?

SSL (Secure Sockets Layer) is a security technology to establish a secure and encrypted connection between a Server and a Browser.

SSL allows sensitive information such as UserId, Password, Debit Card Number, credit card numbers, social security numbers, and login credentials to be transmitted securely.

Actually, the data sent between the Browsers to Web Servers is in plain text, which will be very easy for hackers to hack and see the information from this. Hence, using this Secure Layer(SSL), all the data passed between the Web Servers and the Browsers remains private and integral. SSL is a security protocol. These protocols have algorithms that will encrypt the data being transmitted.

What is an SSL Certificate?

To create an SSL connection, a Web Server requires an SSL Certificate. Certificates are mainly used to communicate with the https protocol. For the http protocol, we don't need a certificate, but for the https protocol, we need a certificate. Hyper Text Transfer Protocol Secure (HTTPS) is the secure version of HTTP, the protocol over which the data is sent between your Client and the Server. The 'S' at the end of HTTPS stands for 'Secure'.

It means communication between your browser and the Website is encrypted.

When you choose to activate SSL on your Web Server, you will be prompted to complete a number of questions about the identity of your Website and your company. Your Web Server then creates two cryptographic keys - a Private Key and a Public Key.

We can say that,

HTTPS=HTTP+S (Secure)

HTTPS always runs in Port No. 443.

Here, I have explained how to create the certificate using makecert.exe.

Here, I am not going to discuss the details of the SSL Certificate but will explain how to read the certificate details inside our code using C#. This topic seems to be very important to me, because I have used Push Notification for iPhone. While working with Push Notification, reading the certificate details is very important.

First of all, we will check our certificate by using a command called "MMC" like this.

mmc command

It will open the console root screen, as shown below.

consolerootscreen

Now, please do the following steps. Click File and Add Remove Snap.

Afterward, choose Certificate and Add.

certificateandadd

Afterward, click Add, and it will ask for the following details.

certificatessnap

These are actually the stores where the certificates are placed. If we want to access these storage locations, then store location.CurrentUser specifies that I want"My user account" store andStoreName.My specifies to the Personal folder in the recent versions of Windows. Hence, if I want to read the certificates from the Personal folder, then I should use storeName.MY.

personal

Now, as you see, my Personal folder has the following certificate. Now, I want to read the certificate details. To show the details, I have the following snapshot with a Gridview.

personalfolder

Now, here is my code to read the certificate details.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CertificateDetails
{
    public partial class Form1 : Form
    {
        DataTable dt = new DataTable();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);
            DataColumn dc1 = new DataColumn("Friendly Name", typeof(string));
            DataColumn dc2 = new DataColumn("ThumbPrint", typeof(string));
            DataColumn dc3 = new DataColumn("SerialNumber", typeof(string));
            DataColumn dc4 = new DataColumn("IssuedBy", typeof(string));
            DataColumn dc5 = new DataColumn("ExpiryDate", typeof(string));
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            dt.Columns.Add(dc4);
            dt.Columns.Add(dc5);
            foreach (X509Certificate2 mCert in store.Certificates)
            {
                dt.Rows.Add(
                    mCert.FriendlyName,
                    mCert.Thumbprint,
                    mCert.SerialNumber,
                    mCert.Issuer,
                    mCert.GetExpirationDateString()
                );
            }
            dataGridView1.DataSource = dt;
        }
    }
}

Now, run the program and check. It will show the details of the certificate as follows.

form1

Thus, in this way, we can read the certificate details in our Application.


Similar Articles