We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

rengber on 10/29/07


Tagged

Certificates Keystore


Versions (?)


Getting X509 Certificates in and out of the Key Store


Published in: C# 


URL: http://support.microsoft.com/default.aspx?scid=kb;en-us;901183

Generate the Cert: makecert -sy 3 -n "cn=FooCorp" -ss my -sr LocalMachine

Grant Permissions to the Cert: winHttpCertCfg -g -c LOCAL_MACHINE\MY -s "FooCorp" -a "domain\YourMomma"

  1. public static X509Certificate2 GetCert(string thumbprint,
  2. StoreName storeName,
  3. StoreLocation storeLocation)
  4. {
  5. // The following code gets the cert from the keystore
  6. X509Store store = new X509Store(storeName, storeLocation);
  7. store.Open(OpenFlags.ReadOnly);
  8. X509Certificate2Collection certCollection =
  9. store.Certificates.Find(X509FindType.FindByThumbprint,
  10. thumbprint,
  11. false);
  12. X509Certificate2Enumerator enumerator = certCollection.GetEnumerator();
  13. X509Certificate2 cert = null;
  14. while (enumerator.MoveNext())
  15. {
  16. cert = enumerator.Current;
  17. }
  18. return cert;
  19. }

Report this snippet 

You need to login to post a comment.