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!

ASP.NET

11 Comments Leave a comment

  1. Hi Isaac Sogunro,
    i want to integrate the CAC with my windows application.
    is there any way to do this as you said above for web application.

    Thanks,
    Sachin

  2. Do I need to initialize the card reader or something to get the client certificates first? I have used the code you posted above, but when I get no output. I set the entireSubjectLine variable equal to the “IsPresent” property and it displays “False”. Thanks for your help.

  3. HI JF, Sorry for the really late reply but if your cac reader is in place and all your certificates, there should be no problem. Anyway, not sure why you would set “entireSubjectLine” equal to ‘IsPresent’ but it’s supposed to contain data from your CAC card. Prior to writing code, your web server must have a valid SSL certificate. Also, make sure “Require Client Certificates” check box is checked. This link might be of some help. http://forums.asp.net/t/1356598.aspx/2/10

  4. I tried the following in vb.net:

    Button click implements this:

    Dim cs As HttpClientCertificate
    Dim entireSubjectLine As String

    cs = Request.ClientCertificate
    entireSubjectLine = cs.Subject.ToString()

    Response.Write(entireSubjectLine)
    Response.End()

    The string is empty. Any idieas what I am doing wrong?

      • I am running into the same problem. The certificate is always empty. Do we need to do any other configuration? You mentioned that our webserver might need to be configured. Any advice on how best to configure it.

  5. I am able to view the card data in ActiveClient. The way I have implemented the code is on our website. I have a blank page with a button that executes the code.

  6. How do you pull the dod email off the CAC? I’ve been searching every where to try to find out how to do this, but I don’t see where the email is stored.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: