public static X509Certificate2 GetCertStorePass(string pin, X509Certificate2 certificate2) { var provider = (Gost3410CryptoServiceProvider)certificate2.PrivateKey; var secure = new System.Security.SecureString(); foreach (char charPass in pin) secure.AppendChar(charPass); provider.SetContainerPassword(secure); return provider.ContainerCertificate; }
c# Get a certificate from a container and set a pin / password programmatically
C# Получаем короткое имя из Subject сертификата
public static string GetSubjectCN(X509Certificate2 cert) { // вырезаем имя из строки int indexStart = cert.Subject.IndexOf("CN"); if (indexStart >= 0) { int indexEnd = cert.Subject.IndexOf(",", indexStart); indexStart += 3; if (indexStart < indexEnd) { return cert.Subject.Substring(indexStart, indexEnd - indexStart); } } return null; }
C# How to get all certificates from "My" Store
public static List<string> GetCertNamesFromStore(string search) { X509Store store = new X509Store("My"); try { store.Open(OpenFlags.ReadOnly); X509Certificate2Collection certCollection = store.Certificates; X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false); var res = new List<string>(); foreach (X509Certificate2 s in certCollection) { if (search=="") res.Add(GetSubjectCN(s)); else if (s.SubjectName.Name.Contains(search)) res.Add(GetSubjectCN(s)); } return res; } finally { store.Close(); } }
Subscribe to:
Posts (Atom)