Webservice Studio 2.0.2 patch

Posted on Thursday, July 15, 2010 by Nicki

Recently a colleague needed to connect to a webservice using Client Certificate authentication. Webservice Studio 2.0.2 does not cater for this, so I did some digging and managed to get it working.

I tried contacting the coordinator listed on http://webservicestudio.codeplex.com/team/view but got no response. I do however want to give it back to the community, so here is the patch you need to apply to 2.0.2.


--- Wsdl.cs Thu Jan 15 11:13:00 1970
+++ Wsdl.cs Thu Jan 15 11:13:00 1970
@@ -19,6 +19,8 @@
using System.Xml.Schema;
using System.Xml.Serialization;

+ using System.Security.Cryptography.X509Certificates;
+
internal class Wsdl
{
private bool cancelled = false;
@@ -292,6 +294,17 @@
{
protocol.Credentials = CredentialCache.DefaultCredentials;
}
+
+ if (this.WsdlProperties.UseClientCert)
+ {
+ X509Certificate2 cert = GetClientCert();
+
+ if (cert != null)
+ {
+ protocol.ClientCertificates.Add(cert);
+ }
+ }
+
if ((this.WsdlProperties.ProxyServer != null) && (this.WsdlProperties.ProxyServer.Length != 0))
{
IWebProxy proxy = null;
@@ -300,6 +313,21 @@
protocol.Proxy = proxy;
}
return protocol;
+ }
+
+ private X509Certificate2 GetClientCert()
+ {
+ X509Store certStore = new X509Store(StoreLocation.CurrentUser);
+ certStore.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
+ X509Certificate2Collection selectedCerts = X509Certificate2UI.SelectFromCollection(certStore.Certificates, "Select a certificate", "Please select the client certificate you would like to use", X509SelectionFlag.SingleSelection);
+
+ certStore.Close();
+
+ if (selectedCerts.Count > 0)
+ {
+ return selectedCerts[0];
+ }
+ return null;
}

private static XmlSchema CreateFakeSoapEncodingSchema(string ns, string name)
--- WsdlProperties.cs Thu Jan 15 11:13:00 1970
+++ WsdlProperties.cs Thu Jan 15 11:13:00 1970
@@ -18,6 +18,14 @@
private string proxyPassword;
private string proxyUserName;
private string userName;
+ private bool useClientCert;
+
+ [XmlAttribute]
+ public bool UseClientCert
+ {
+ get { return useClientCert; }
+ set { useClientCert = value; }
+ }

public string[] GetProxyBaseTypeList()
{