How to: Migrate Code that Uses X509SecurityToken Security Tokens from WSE 2.0 to 3.0
To use X509SecurityToken security tokens in the Web Services Enhancements 3.0 for Microsoft .NET (WSE), you use the certificate classes in the .NET Framework 2.0. The following procedures detail how to migrate WSE 2.0 code that retrieved certificates using the WSE API to now use the .NET Framework classes.
To use the .NET Framework classes to retrieve certificates
Add a reference to the System.Security assembly.
- In Solution Explorer, right-click the project name, and then click Add Reference.
- Click the .NET tab, click System.Security.dll.
- Click OK.
Add an Imports statement or using directive for System.Security.Cryptography.X509Certificates to the top of the file that uses X509SecurityToken security tokens.
Replace code that uses X509CertificateStore with System.Security.Cryptography.X509Certificates.X509Store.
The following code examples show how to migrate WSE 2.0 code that retrieves an X.509 certificate from X509CertificateStore to instead use System.Security.Cryptography.X509Certificates.X509Store.
WSE 2.0
Dim store As X509CertificateStore = _ X509CertificateStore.CurrentUserStore( _ X509CertificateStore.MyStore) Dim open As Boolean = store.OpenRead()
X509CertificateStore store = X509CertificateStore.CurrentUserStore( X509CertificateStore.MyStore); bool open = store.OpenRead();
WSE 3.0
Dim store As X509Store = New X509Store(StoreName.My, _ StoreLocation.CurrentUser) store.Open(OpenFlags.ReadOnly)
X509SecurityToken securityToken = null; X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly);
Replace X509CertificateCollection with System.Security.Cryptography.X509Certificates.X509Certificate2Collection.
The following code examples show how to migrate WSE 2.0 code that searches the certificate store to return a collection of certificates.
WSE 2.0
Dim certs As X509CertificateCollection = _ store.FindCertificateByHash(certHash)
X509CertificateCollection certs = store.FindCertificateByHash(certHash);
WSE 3.0
Dim certs As X509Certificate2Collection = _ store.Certificates.Find(X509FindType.FindBySubjectName, _ subjectName, False)
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, certHash, false);
Replace X509Certificate with System.Security.Cryptography.X509Certificates.X509Certificate2.
The following code examples show how to migrate WSE 2.0 code that retrieves a certificate from a certificate collection.
WSE 2.0
If certs.Count = 1 Then Dim cert As _ Microsoft.Web.Services2.Security.X509.X509Certificate = _ CType(certs(0), Microsoft.Web.Services2.Security.X509.X509Certificate) securityToken = New X509SecurityToken(cert)
if (certs.Count == 1) { Microsoft.Web.Services3.Security.X509.X509Certificate cert = ((Microsoft.Web.Services3.Security.X509.X509Certificate) certs[0]); securityToken = new X509SecurityToken(cert); }
WSE 3.0
Dim cert As X509Certificate2 If certs.Count = 1 Then cert = certs(0) securityToken = New X509SecurityToken(cert)
X509Certificate2 cert; if (certs.Count == 1) { cert = certs[0]; securityToken = new X509SecurityToken(cert); }
See Also
Tasks
How to: Migrate Code that Secures a SOAP Message without Policy to Use Custom Policy Assertions