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.
“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!
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
Sorry for the late response. I have not done much with windows application but I don’t see why the same code won’t work for a windows application.
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.
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
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?
If you CAC card is inserted, it shouldn’t be blank. If you click on your Active Client Agent, are you able to see the data on your CAC card?
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.
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.
I am not sure where you are getting your information, but good topic.
I needs to spend some time learning more or understanding more.
Thanks for excellent info I was looking for this information for my mission.
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.
If it’s not part of the clientcertificate data, I’m not sure how. Sorry!