public static void GetTokenInfo() { Program.logLine("Starting GetTokenInfo"); X509Store store = new X509Store("My"); Program.logLine(store.ToString()); store.Open(OpenFlags.ReadOnly); foreach (X509Certificate2 cert2 in store.Certificates) { Program.logLine(cert2.ToString()); if (cert2.HasPrivateKey) { Program.logLine("Has Private Key"); try { RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert2.PrivateKey; if (rsa == null) continue; // not smart card cert again if (rsa.CspKeyContainerInfo.HardwareDevice) // sure - smartcard { Console.WriteLine("======================================================================="); Console.WriteLine("Issuer: " + cert2.Issuer); Console.WriteLine("Subject: " + cert2.Subject); Console.WriteLine("Serial: " + cert2.SerialNumber); Console.WriteLine("ProviderName: " + rsa.CspKeyContainerInfo.ProviderName); Console.WriteLine("KeyContainerName: " + rsa.CspKeyContainerInfo.KeyContainerName); foreach (X509Extension extension in cert2.Extensions) { if (extension.Oid.FriendlyName == "Key Usage") { X509KeyUsageExtension ext = (X509KeyUsageExtension)extension; Console.WriteLine("Key Usage: " + ext.KeyUsages); } } } } catch (CryptographicException c) { Console.WriteLine("Serial: " + cert2.SerialNumber); Console.WriteLine("No se tiene acceso a la clave privada" + c.ToString()); } } } }
/// <summary> /// Returns an <see cref="ITrac"/> instance which is connected to a <see cref="ServerDetails"/> object. /// </summary> /// <param name="serverDetails"></param> /// <returns></returns> public static ITrac GetTrac(ServerDetails serverDetails) { ITrac trac = XmlRpcProxyGen.Create<ITrac>(); trac.Proxy = WebRequest.DefaultWebProxy; trac.Url = serverDetails.XmlRpcUrl(); switch (serverDetails.RequiredAuthentication) { case AuthenticationTypes.BasicAuthentication: trac.Credentials = new NetworkCredential(serverDetails.Username, serverDetails.Password); break; case AuthenticationTypes.IntegratedAuthentication: trac.Credentials = CredentialCache.DefaultNetworkCredentials; break; case AuthenticationTypes.ClientCertAuthentication: try { X509Store s = new X509Store(StoreName.My, StoreLocation.CurrentUser); X509Certificate2Collection col; s.Open(OpenFlags.ReadOnly); col = s.Certificates.Find(X509FindType.FindBySubjectName, serverDetails.Username, true); if (col.Count == 1) { trac.ClientCertificates.Add(col[0]); } else { System.Windows.Forms.MessageBox.Show("No or multiple (" + col.Count + ") certificate with name [" + serverDetails.Username + "] found."); } s.Close(); } catch (System.Security.Cryptography.CryptographicException s) { System.Windows.Forms.MessageBox.Show("CryptographicException: " + s.ToString()); } catch (System.Security.SecurityException s) { System.Windows.Forms.MessageBox.Show("SecurityException: " + s.ToString()); } catch (System.ArgumentException s) { System.Windows.Forms.MessageBox.Show("ArgumentException: " + s.ToString()); } break; case AuthenticationTypes.None: trac.Credentials = null; break; } return trac; }