【问题标题】:Using certificate file to connect to webservice over SSL使用证书文件通过 SSL 连接到 Web 服务
【发布时间】:2012-02-04 05:14:22
【问题描述】:

我正在用 C# 开发调用 web 服务方法的 windows 服务。我必须使用 SSL 连接到 web 服务。我收到了带有证书的发布者 p12 文件。该文件受密码保护。要使用 Import 方法来使用此证书。一切正常,但我不喜欢这种方法 - 我在我的应用程序中设置了密码。当发布者更改证书时,我必须重写代码(将密码更改为新密码)。有没有办法不将密码硬编码到 .p12 文件或使用其他选项(.cer 文件)?

【问题讨论】:

    标签: c# .net ssl windows-services ssl-certificate


    【解决方案1】:

    你可以这样做:

    1. 将 SSL 证书安装到本地计算机证书存储区(使用 Microsoft 管理控制台“MMC”)
    2. 提取证书指纹(例如“748681ca3646ccc7c4facb7360a0e3baa0894cb5”)
    3. 使用从本地证书存储中获取给定指纹的证书的函数。
    4. 调用您的网络服务时提供 SSL 证书。
    private static X509Certificate2 GetCertificateByThumbprint(string certificateThumbPrint, StoreLocation certificateStoreLocation) {
        X509Certificate2 certificate = null;
    
        X509Store certificateStore = new X509Store(certificateStoreLocation);
        certificateStore.Open(OpenFlags.ReadOnly);
    
    
        X509Certificate2Collection certCollection = certificateStore.Certificates;
        foreach (X509Certificate2 cert in certCollection)
        {
            if (cert.Thumbprint != null && cert.Thumbprint.Equals(certificateThumbPrint, StringComparison.OrdinalIgnoreCase))
            {
                certificate = cert;
                break;
            }
        }
    
        if (certificate == null)
        {
            Log.ErrorFormat(CultureInfo.InvariantCulture, "Certificate with thumbprint {0} not found", certificateThumbPrint);
        }
    
        return certificate;
    }
    
    public string GetServiceResponse() {
        string WebSvcEndpointConfigurationName = "WebServiceEndpoint";
        Uri webSvcEndpointAddress = new Uri("http://www.example.com/YourWebService.svc");
        string webSvcCertificateThumbPrint = "748681ca3646ccc7c4facb7360a0e3baa0894cb5";
    
        string webSvcResponse = null;
        SomeWebServiceClient webServiceClient = null;
    
        try
        {
            webServiceClient = new SomeWebServiceClient(WebSvcEndpointConfigurationName, new EndpointAddress(webSvcEndpointAddress));
            webServiceClient.ClientCredentials.ClientCertificate.Certificate = GetCertificateByThumbprint(webSvcCertificateThumbPrint, StoreLocation.LocalMachine);
    
            webSvcResponse = webServiceClient.GetServiceResponse();
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (webServiceClient != null)
            {
                webServiceClient.Close();
            }
        }
        return webSvcResponse;
    } 
    

    【讨论】:

    • 您好,请问我应该为 SomeWebServiceClient 类添加什么?我添加了一个名为“PlanningService”的服务引用类(一个 wsdl 服务引用),它没有任何名为 ClientCredentials 的属性。类是否需要是某种特定对象的类型?谢谢。
    【解决方案2】:

    PKCS#12 文件提供给您,因为它是传输证书和私钥的自然方式。您可以使用以下方法之一:

    • 将其转换为您喜欢的格式并以您喜欢的方式存储
    • 将其转换为无密码 PFX
    • 将其导入计算机的证书存储并以这种方式使用它

    但是所有这些方法(连同保留硬编码的密码)都没有为私钥提供真正的保护,因此如果您将应用程序分发到组织外部,则无法使用。

    【讨论】:

      猜你喜欢
      • 2016-07-12
      • 1970-01-01
      • 2011-12-31
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      • 2019-01-05
      相关资源
      最近更新 更多