Wednesday, March 30, 2005

Using an X.509 Certificate in WSE 2.0

The following code shows how to wrap an X.509 certificate in WSE 2.0.

using Microsoft.Web.Services2.Security.X509;

public static X509Certificate GetCertificate(string subjectString)
{

// create an X509 certificate store and open it for read.
X509CertificateStore store = X509CertificateStore.CurrentUserStore(X509CertificateStore.MyStore);
bool open = store.OpenRead();
if (open == false)
return null;


// Finds a X509Certificate object in the store using the certificate's name value.
X509CertificateCollection certs = store.FindCertificateBySubjectString(subjectString);
store.Close();
X509Certificate cert = (X509Certificate)certs[0];


return cert;
}

X509CertificateStore.MyStore represents the predefined system certificate store "My". This field is constant. The subjectString is the certificate's name value.


References:
Michael Stiefel, Securing Service Oriented Architecture with WSE 2.0