如何在c#中从SmartCard读取凭据

在我的组织中,用户必须使用SmartCard进行交互式登录Windows工作站(95,Vista和7)。 几乎每天,我们都需要读取存储在SmartCard中的凭据,并将它们与ActiveDirectory进行比较,而无需实现自定义凭证管理器。 我们比较的字段是:userPrincialName和sAMAccountName。

你能告诉我一个代码,演示如何从智能卡读取凭据或引导我访问互联网上的文章/代码吗?

对互联网的搜索建议实施凭证管理器或使用其他语言(如C,C ++)。 另外,我遇​​到了这篇文章: http : //www.codeproject.com/Articles/17013/Smart-Card-Framework-for-NET由orouit编写,这是一个使用智能卡的框架 – 但我认为这太多了为了我的简单任务。 你怎么看?

好吧,如果在Windows下开发,一旦你插入智能卡窗口将从智能卡获取所有证书将它们放到我的证书存储区。

var smartCardCerts = new List(); var myStore = new X509Store(StoreName.My, StoreLocation.CurrentUser); foreach(X509Certificate2 cert in myStore) { if( !cert.HasPrivateKey ) continue; // not smartcard for sure var rsa = cert.PrivateKey as RSACryptoServiceProvider; if( rsa==null ) continue; // not smart card cert again if( rsa.CspKeyContainerInfo.HardwareDevice ) // sure - smartcard { // inspect rsa.CspKeyContainerInfo.KeyContainerName Property // or rsa.CspKeyContainerInfo.ProviderName (your smartcard provider, such as // "Schlumberger Cryptographic Service Provider" for Schlumberger Cryptoflex 4K // card, etc var name = cert.Name; rsa.SignData(); // to confirm presence of private key - to finally authenticate } } 

现在基本上可以通过.NET获得很多加密API。 但您也可以直接使用API 加密API

例如,您可以直接通过智能卡访问

 CryptAcquireContext(&hProv,"\\.\\",...) 

读者名称是读卡器名称,容器名称是上面代码片段中的rsa.KeyContainerName。 有多种方法可以访问这样的信息,而且Crypto API不是非常一致或直接。 作为提示,CryptAcquireContext的.NET版本是带有CspParameters的RSACryptoServiceProvider,您可以根据需要指定容器名称。

在ActiveDirectory中查找用户可以通过System.DirectoryServices.DirectoyEntry和System.DirectoryServices.DirectorySearcher完成,但不要忘记System.DirectoryServices.ActiveDirectory.Forest和相关的API,这使得一些事情更容易弄清楚。

你可以得到