Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read the Common Name from the client certificate?

Our application needs a piece of data that it is included in the client cert's common name. Currently, I'm trying to get it from HttpContext.Current.Request.ClientCertificate. How do I read this out? Sadly, I'm trying to code this blind while I figure out why SoapUI isn't sending the cert, so I haven't tried much other than reading about the object on MSDN and poking through the empty properties but I'm not sure what I'm looking for. So to recap, what do I need to do to pull out the common name from this cert? TIA

like image 463
Sinaesthetic Avatar asked Sep 04 '14 23:09

Sinaesthetic


People also ask

What is common name in client certificate?

The Common Name (AKA CN) represents the server name protected by the SSL certificate. The certificate is valid only if the request hostname matches the certificate common name. Most web browsers display a warning message when connecting to an address that does not match the common name in the certificate.

How do I find my common name in CSR?

While generating a CSR, you will be required to enter information in the Common Name field. Certificates are specific to the Common Name that they have been issued to at the Host level. The Common Name must be the same as the Web address you access when connecting to a secure site.

What is O and CN in certificate?

Some of the most common RDNs and their explanations are as follows: CN : CommonName. OU : OrganizationalUnit. O : Organization.


1 Answers

I am maybe too late to answer your question but i hope this would help others who are looking for the way to get common name from certificate.

If you use 'Subject', you might need to trim away other unnecessary information. For example, CN = localhost,OU = DepartmentName,O = CompanyName,L = Location,S = State,C = Country

Dim store As New X509Store(StoreName.My, StoreLocation.LocalMachine)
store.Open(OpenFlags.ReadOnly)
store.Certificates(0).Subject

But if you use the code below, you will get 'localhost' which directly give you the common name of the certificate.

Dim store As New X509Store(StoreName.My, StoreLocation.LocalMachine)
store.Open(OpenFlags.ReadOnly)
store.Certificates(0).GetNameInfo(X509NameType.SimpleName, False)

Here's the link for reference:- https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate2.getnameinfo(v=vs.110).aspx

like image 173
Yew Hong Tat Avatar answered Oct 03 '22 03:10

Yew Hong Tat