Categories
ASP.NET

CAC Card Implementation in ASP.NET

For those of us that have ever worked for the government, there are times in ASP.NET when you will want to read Common Access Card (CAC) information and use it for authentication in our .NET web applications.  If you have ever wondered how to get CAC information using .NET,
here’s some C# code that shows how to read data from a CAC card​: 

C#

//Get certificate data
HttpClientCertificate cs = Request.ClientCertificate;
string entireSubjectLine = cs.Subject.ToString();

The result of “entireSubjectLine” variable will be the following
C=US, O=U.S. Government, OU=OGC, OU=PKI, OU=CONTRACTOR, CN=LastName.FirstName.MI.1234567890
You should then be able to parse through the “entireSubjectLine” variable to retrieve the data you want to use in your code.  The last number in the “entireSubjectLine” string (1234567890) will be the CAC number which is unique for every CAC.

That was simple.  Happy Coding!