Open Certificate Stores Including Certificates in Token

Open Certificate Stores Including Certificates in Token

In my article Mirror Link I have shown how to open a certificate store (Machine certificate store which is displayed in the Internet Explorer.) but the drawback of that code was it can't open the certificates stored in the token. so here is another method which will overcome the drawback stated above. It will open the certificates of the machine store as well as of the tokens.

/// <summary>

/// Opens the Certificate Store of IE including the Certificates in Token

/// </summary>

/// <param name="popupScript">The variable passed to store the reason if function returns false</param>

/// <returns> True if a certificate is selected and false if no certificate is selected</returns>

public Boolean OpenStoreToken(ref string popupScript)


x509_2 = null;

//Create and Initilaize a variable of x509Store providing the store name and the store location

X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);

//Create X509Certificate2Collection

X509Certificate2Collection col = new X509Certificate2Collection();

//Create X509Certificate2Collection

X509Certificate2Collection sel = new X509Certificate2Collection();

//Create X509Certificate2Enumerator

X509Certificate2Enumerator en;// =new X509Certificate2Enumerator();

//Open the Store for readonly purpose


//set the col i.e. X509Certificate2Collection to the collection of the certificates stored in the IE and the Token

col = st.Certificates;

//set the sel i.e. X509Certificate2Collection which actuly displays a dialog box for selecting an X.509 certificate from a certificate collection.

sel = X509Certificate2UI.SelectFromCollection(col, "Certificates", "Select one to sign", X509SelectionFlag.SingleSelection);

if (sel.Count > 0)


en = sel.GetEnumerator();


x509_2 = en.Current;



if (x509_2 == null)


popupScript = "You didn't select a certificate!!!";

return false;




return true;



Thanks and Regards
Meetu Choudhary



Author: Vinay Singh03 Sep 2010 Member Level: Gold   Points : 1


I am getting error with X509Certificate2UI.

Error 1 The name 'X509Certificate2UI' does not exist in the current context D:\Data\Websites\TestWMW\GUI\Cert\GetCert.aspx.cs 48 21 D:\...\GUI\

Can you please how to resolve the issue

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: