且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用客户端证书验证从winforms应用程序实现Web服务(asmx)

更新时间:2022-05-13 22:15:56

请参阅: http://technet.microsoft.com/en-us/library/cc732996 %28v = WS.10%29.aspx [ ^ ]。



-SA


好吧,我找不到任何有端到端解决方案的文章,所以我不得不继续推进我自己的研究/试错法。



为了试用,我为我的域名购买了免费的SSL证书(www.mydomain.com)。



注意:COMODO网站提供90天免费SSL证书(点击此处 [ ^ ])。他们的支持团队非常棒,可以帮助您解决任何安装问题。您还需要联系您的托管服务提供商以生成CSR(证书签名请求)并提供给您。您可以将CSR带到comodo网站并提交证书订单。您的证书将很快通过电子邮件发送给您。



因此,我通过联系我的托管服务提供商的支持团队在我的网站上安装了证书。我测试了我的网站,看到了锁定的图标和证书信息。到目前为止一直很好。



现在,我想使用客户端证书文件(mydomain.cer)来调用我的网站上托管的Web服务,该服务将从Windows客户端调用。我添加了以下代码。



Well, I could not find any articles with end to end solution so I had to move forward with my own research/trial-error method.

To try out, I bought the FREE SSL certificate for my domain (www.mydomain.com).

Note: Free SSL Certificate for 90 Days is available from COMODO website (click here[^]). Their support team is great and help you troubleshoot any issues for installation etc. You will also need to contact your hosting provider to generate CSR (Certificate Signing Request) and provide to you. You can take the CSR to comodo website and submit an order for cert. Your cert will be emailed to you soon.

So, I installed the certificate on my site by contacting support team of my hosting provider. I tested my site and see the locked icon and certificate information. So far so good.

Now, I wanted to use client certificate file (mydomain.cer) to call web service hosted on my website which will be called from windows client. I added following code.

string certPath = "C:\\MyProjects\\WinApp1\\mydomain.cer";
X509Certificate cert = X509Certificate.CreateFromCertFile(certPath);





调试时,我在第二行发现了以下错误。



CryptographicException:索引值无效。



我也找不到任何关于谷歌下面的错误的具体信息:(所以我联系了Comodo支持团队。他们发给我另一个文件扩展名.CRT而不是.CER。



所以,我对代码应用了以下更改并且它有效。





While debugging, I found the following error on 2nd line.

CryptographicException: "The index value is not valid."

I could not find anything specific about below error on google too :( so I contacted Comodo support team. They sent me another file with extension .CRT instead of .CER.

So, I applied following changes to the code and it worked.

string certPath = "C:\\MyProjects\\WinApp1\\mydomain.CRT";
X509Certificate cert = X509Certificate.CreateFromCertFile(certPath);





接下来,我添加了添加证书的代码并调用Web服务方法。





Next, I added code to add certificate and call the web service method.

service.ClientCertificates.Add(cert);
string resultString = service.ServiceMethod(param1);





但是,我收到以下错误时间。



System.Web.Services.Protocols.SoapException:服务器无法识别HTTP标头SOAPAction的值:



所以,我发现我必须更新我的winforms应用程序中的web服务引用可能是bcoz网站现在包含SSL证书和更改等。



右键单击Web Reference并单击更新Web引用。



应用更改后,我可以使用Web服务调用客户证书,使用下面的代码。





However, I got the following error this time.

System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction:

So, I found that I had to update the web service reference in my winforms application may be bcoz the website now contains SSL certificate and changes etc.

Right Click on Web Reference and click on "Update Web Reference".

After applying changes, I was able to call web service using Client Certificate, using below code.

string certPath = "C:\\MyProjects\\WinApp1\\mydomain.CRT";
X509Certificate cert = X509Certificate.CreateFromCertFile(certPath);

service.ClientCertificates.Add(cert);
string resultString = service.ServiceMethod(param1);





希望它对某人有帮助。



Hope it helps someone.