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 the sensitive information such as UserId, Password, Debit Card Number, credit card numbers, social security numbers and the 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 the hackers to hack and see the information from this. Hence, using this Secure Layer(SSL), all the data is passed between the Web Servers and the Browsers remain private and integral. SSL is a security protocol. These protocols have the algorithms which will encript the data being transmitted.

What is an SSL Certificate ?
 
To create SSL connection, a Web Server requires an SSL Certificate. Certificates are mainly used to communicate with the https protocol. For http protocol, we don't need a certificate but for 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 the 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 run in Port No. 443.

Here, I have explained how to create the certificate, using makecert.exe. Please check the Link.
 
Here, I am not going to discuss the details of SSL Certificate but will explain how to read the certificate details inside our code, using C#. This topic seems to be very important for me, because I have used Push Notification for IPhone. While working with Push Notification, reading the certificate details are very important.
 
First of all, we will check our certificate by using a command called "MMC" like this.



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



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

 

Afterwards, choose Certificate and Add.



Afterwards, click Add and it will ask for the following details:

 

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

 

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:
 
 

Now, here is my code to read the certificate details: 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Security.Cryptography.X509Certificates;  
  8. using System.Text;  
  9. using System.Threading.Tasks;  
  10. using System.Windows.Forms;  
  11. using System;  
  12. using System.Security.Cryptography.X509Certificates;  
  13. using System.Security.Cryptography;  
  14. using System.Data;  
  15.    
  16.   
  17. namespace CertificateDetails  
  18. {  
  19.     public partial class Form1 : Form  
  20.     {  
  21.         DataTable dt = new DataTable();  
  22.         public Form1()  
  23.         {  
  24.             InitializeComponent();  
  25.         }  
  26.   
  27.         private void Form1_Load(object sender, EventArgs e)  
  28.         {  
  29.             var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);  
  30.             store.Open(OpenFlags.ReadOnly);  
  31.   
  32.             DataColumn dc1 = new DataColumn("Friendly Name"typeof(string));  
  33.             DataColumn dc2 = new DataColumn("ThumbPrint"typeof(string));  
  34.             DataColumn dc3 = new DataColumn("SerialNumber"typeof(string));  
  35.             DataColumn dc4 = new DataColumn("IssuedBy"typeof(string));  
  36.             DataColumn dc5 = new DataColumn("ExpairedDate"typeof(string));  
  37.             dt.Columns.Add(dc1);  
  38.             dt.Columns.Add(dc2);  
  39.             dt.Columns.Add(dc3);  
  40.             dt.Columns.Add(dc4);  
  41.             dt.Columns.Add(dc5);  
  42.             
  43.   
  44.             foreach (X509Certificate2 mCert in store.Certificates)  
  45.             {  
  46.                 dt.Rows.Add(mCert.FriendlyName,mCert.Thumbprint,mCert.SerialNumber,mCert.GetExpirationDateString());   
  47.                     
  48.             }  
  49.   
  50.             dataGridView1.DataSource = dt;   
  51.   
  52.         }  
  53.   
  54.     }  
  55. }  
Now, run the program and check. It will show the details of the certificate, as follows:



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

Up Next
    Ebook Download
    View all
    Learn
    View all