简体   繁体   中英

How can I get the X509Certificate sent from the client in web service?

I am trying to get client certificate in web service.

  1. I add client certificate in proxy object and call web method Verify .

     private void button2_Click(object sender, EventArgs e) { string certPath = "D:\\\\test.pfx"; cert = new X509Certificate2(certPath,"pass"); Service1 obj = new Service1(); obj.ClientCertificates.Add(cert); textBox2.Text = obj.Verify(); } 

Web Method in Web Service:

[WebMethod]
public string Verify()
{
    X509Certificate2 cert = new X509Certificate2(Context.Request.ClientCertificate.Certificate);
    bool test = cert.Verify();
    return test.ToString();

}

In finish with this error:

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Security.Cryptography.CryptographicException: m_safeCertContext is an invalid handle.

I don't know why. I used this sample .

I test web service and client on localhost.

Have you tried to follow this guide ?

  • Host the project from IIS - not the thin web server that is bundled with VS2005.
    • From within VS2005 select File - New Web Site.
    • Select the Location of HTTP then enter the path eg: http://localhost/MyWebSite . Note that you don't have to use HTTPS just yet (I find it easier for development to use HTTP then when deploying to UAT or Production to use HTTPS).
  • Code up a test form.
  • Go into IIS Admin - right click on the new app (MyWebSite) and select Properties.
  • On the Directory Security tab, click Edit... under 'Secure communications'.
  • Make sure 'Accept client certificates' is checked.
  • When you run your app - make sure you use HTTPS in the url eg: https://localhost/MyWebSite

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM