Attach an X509 client certificate to a web service request based on the certificates FriendlyName1.)
//using
using System.Security.Cryptography.X509Certificates;
2.)
//Create web service
WS_Service service = new WS_Service();
//Add certificate
service.ClientCertificates.Add(GetCertificate(certificateName));
service.ExecuteServiceRequest();
3.)
//Adding Certificate
private X509Certificate GetCertificate(string certName)
{
// Look in the local machine store.
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509CertificateCollection col = (X509CertificateCollection)store.Certificates;
X509Certificate cert = null;
for (int i = 0; i < col.Count; i++)
{
string friendlyName = ((X509Certificate2)col[i]).FriendlyName;
if (friendlyName.ToLower().Trim() == certName.ToLower().Trim())
{
cert = col[i];
break;
}
}
return cert;
}
No comments:
Post a Comment