Title: Smartcard authentication for .NET
Tags: .net;authentication;smartcard
Question: I have a smartcard reader and a smartcard. I have installed drivers and it works as expected. I can use this card as windows logon or for remote desktop logon. 

I'm building my app which should work only when the card is inserted and I need to call web services from my app which requires a certificate from the card.

Any suggestions on how do I do that? The web is full of examples for ASP.NET and I'm building Windows forms.

As a further note: Everything must work the same even if user logs on windows without the card. The card must me present for the app to work.

Thanks.

Comment: Have a splash screen, do your attempt at authentication during the time, if the authentication fails close the application.  You should be able to easily use the examples you found and apply them to your application.

Here is the accepted answer: If the Smartcard driver supports the standard Windows CryptoAPI, it will export the certificates from the card into the personal store of the user. You can access those certificates using the X509Store class. When you access the certificate, the user will be prompted to insert the card and enter his PIN.

Note: Some smartcard drivers do not automatically export the certificates. Instead, they have a tool which the user can use to do this.

Comment for this answer: I already did that, thanks. Since I have several cards for testing, all of them are in the X509Store(StoreName.My). I'm not sure what is happening when the user inserts the card. I should somehow find out which certificate is 'activated'?
Comment for this answer: I don't know which certificate I want. I want to identify the user using his smartcard.
Comment for this answer: User identification should be automatic if the smartcard is inserted. If there is no smartcard application has to display a message to insert the card into a reader. This is my task and required behaviour closely resembles Windows log on using smartcards.
Comment for this answer: I will trust all certificates. Once I have the certificate in my system I can extract required information to see if any of my users own this certificate. Then, I'll use the certificate so that user can log on with his PIN. I just don't know how to get the certificate from the smartcard.
Comment for this answer: @HenningKrause: are you trying to filter the certificates like this: http://stackoverflow.com/questions/7555281/smartcard-authentication-for-net ?
Comment for this answer: Well, since you want to authenticate the user, you should know in advance which certificate you want. When you access a certificate from a smartcard which is not attached to the computer, the user is prompted to insert the card. (Unless you specify the Silent flag in some of the crypto operations - in this case you simply get an error.)
Comment for this answer: But in the end, the user could present any certificate. You'll need to have some indication wether the certificate presented is valid or not. The usual step would be to present the user a list of certificates (using http://msdn.microsoft.com/de-de/library/system.security.cryptography.x509certificates.x509certificate2ui.aspx fore example). Then the user selects his certificates and you validate it according to your rules.
Comment for this answer: Yeah.. but the certificate used by WIndows for smartcard logon has special information in it. The user principal name, for example. And it has to be trusted by your organization. So, Windows can deduct from the certificate alone who you are. You have to establish something similar. Either by trusting all certificates form a specific CA or by some sort of sign-up procedure.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:09/26/11', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<.net><authentication><smartcard>', 'Title': 'Smartcard authentication for .NET', 'ViewCount': 3847, 'id': 7555281, 'user_AboutMe': '', 'user_AccountId': 441315.0, 'user_DisplayName': 'Nezreli', 'user_Reputation': 1248.0, 'user_Views': 105.0, 'user_id': 831338.0}}