Подпись ИЭМК с помощью VipNet CSP
Поиск нужного сертификата. Ищем по конкретному СНИЛСу и действующий на текущую дату.
X509Certificate2 CertItog = find_certificate_();
public static X509Certificate2 find_certificate_()
{
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2 cert = new X509Certificate2();
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
string str_SN = "";
foreach (X509Certificate2 x509 in fcollection)
{
try
{
if (x509.Subject.ToUpper().Contains("SNILS") || x509.Subject.ToUpper().Contains("СНИЛС"))
{
str_SN = x509.Subject.Substring(x509.Subject.IndexOf("SNILS") + 6, 11);
int n;
if (!int.TryParse(str_SN.Substring(0, 10), out n))
{
str_SN = x509.Subject.Substring(x509.Subject.ToUpper().IndexOf("СНИЛС") + 6, 11);
if (!int.TryParse(str_SN.Substring(0, 10), out n))
{
continue;
// System.Windows.Forms.MessageBox.Show("Снилс в сертификате не найден или пустой");
}
}
if (String.CompareOrdinal(str_SN, "NNNNNNNNNNNNN") == 0)
{
cert = x509;
// Console.WriteLine(cert.Subject);
Console.WriteLine(str_SN);
// Console.ReadKey();
break;
}
}
x509.Reset();
}
catch (CryptographicException)
{
Console.WriteLine("Information could not be written out for this certificate.");
}
}
store.Close();
return cert;
}