【问题标题】:How use certificate to call soap API in ASP.Net?ASP.Net中如何使用证书调用soap API?
【发布时间】:2015-10-29 03:04:09
【问题描述】:

我想使用soap API 从系统中提取一些数据。 API 开发人员为我提供了两个证书,并要求我在代码中使用它们。他说如果我在调用 API 时不使用它们,我将无法获得完整的数据集。这是我第一次想使用证书来调用soap API,我没有任何想法。我在互联网上进行了一些搜索,但找不到任何可以帮助我的东西。 这是我的申请详情: 1. ASP.NET 2.C# 3..Net框架4.0

【问题讨论】:

    标签: c# asp.net ssl soap


    【解决方案1】:

    假设这是一个 X509 证书,您将生成对 CERT 文件的引用,然后将其添加到 WebRequest 对象的 ClientCertificates 属性中。

    //You must change the path to point to your .cer file location. 
    X509Certificate Cert = X509Certificate.CreateFromCertFile("C:\\mycert.cer");
    // Handle any certificate errors on the certificate from the server.
    ServicePointManager.CertificatePolicy = new CertPolicy();
    // You must change the URL to point to your Web server.
    HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://YourServer/sample.asp");
    Request.ClientCertificates.Add(Cert);
    Request.UserAgent = "Client Cert Sample";
    Request.Method = "GET";
    HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
    

    更多详情,请到:https://support.microsoft.com/en-us/kb/895971

    有时 CERT 文件受密码保护。这是 VB.Net 代码,但是当文件上有密码时,它会生成 CERT 对象:

    Dim fs As FileStream = File.Open(certfilePath, FileMode.Open, FileAccess.Read)
    Dim buffer As Byte()
    ReDim buffer(fs.Length)
    Dim count As Integer = fs.Read(buffer, 0, buffer.Length)
    fs.Close()
    Dim cert As X509Certificate2 = New X509Certificate2(buffer, passwordToFile)
    

    另请注意,如果您使用 SoapHttpClientProtocol(或将其继承到特定类以使用此 SOAP API,其他 System.Web.Protocol 元素也是如此),则它还包含 ClientCertificates 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 2015-05-03
      • 2023-04-08
      • 1970-01-01
      • 2021-06-08
      • 2019-09-17
      相关资源
      最近更新 更多