Passing Client Certificate for authentication to Web Service in ASP.NET Method

To pass Client Certificate for authentication to Web Service in ASP.NET Method , we need to use Web Service Enhancements 2.0 or 3.0 .

Below code shows passing client certificate using WSE 2.0 :

WSE 2.0 method
// Get certificates under Personal in Local Machine Store
X509CertificateStore store =
X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore);
store.OpenRead();
// Get Certificate Collection with below Subject
X509CertificateCollection col =
(
X509CertificateCollection)store.FindCertificateBySubjectString(“certName”);
X509Certificate cert = null;
cert = col[0];
// Add Certificate to Web Service to pass it
jobService.ClientCertificates.Add(cert);

Below code shows passing client certificate using WSE 3.0 :
WSE 3.0 method

X509Storestore = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(
OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByIssuerName, certHash, false);
if (certs.Count == 1)
{X509Certificate2 cert =
((
X509Certificate2)certs[0]);
}

// Add Certificate to Web Service to pass it
jobService.ClientCertificates.Add(cert);

WSE 3.0 is used to find client certificates in Local Machine and Personal store where as WSE 2.0 is used to find client certificates in Local Machine store but it cannot find certificates in Personal store.

    

Leave a Reply

%d bloggers like this: