static X509Certificate2 GetCertificate(string thumbPrint) { List locations = new List { StoreLocation.CurrentUser, StoreLocation.LocalMachine }; foreach (var location in locations) { X509Store store = new X509Store(StoreName.My, location); try { store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection certificates = store.Certificates.Find( X509FindType.FindByThumbprint, thumbPrint, true); if (certificates.Count >= 1) { return certificates[0]; } } finally { store.Close(); } } throw new ArgumentException($"A Certificate with Thumbprint '{thumbPrint}' could not be located."); }