使用SSL从C#应用程序调用WebService

时间:2011-04-13 11:42:37

标签: c# web-services ssl

在我的C#桌面应用程序中,我正在调用php开发的Web服务。我可以拥有CA文件。我不知道如何通过SSL调用Web服务并验证证书。我需要通过服务器以及从服务器进行身份验证的响应内容?老实说,我不知道。

编辑: 参考:http://weblogs.asp.net/jan/archive/2003/12/04/41154.aspx

//在调用Web服务之前     System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();

public class MyPolicy : ICertificatePolicy
{
    X509Certificate clientCert = null;

    public MyPolicy() {
        clientCert = X509Certificate.CreateFromSignedFile(HTTPUtility.CERT_FILE);
    }

    public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
    {
        Console.WriteLine("********* Into CheckValidationResult : " + certificate.ToString());

        Console.WriteLine("####### Client Certificate : " + clientCert.ToString() + "\n" + "Subject = " + clientCert.Subject);
        Console.WriteLine("Issuer : " + clientCert.Issuer + "\n Seral No : " + clientCert.GetSerialNumberString());
        Console.WriteLine("Not Before : " + clientCert.GetEffectiveDateString() +" \n Not After : " + clientCert.GetExpirationDateString());
        Console.WriteLine("Thumb Print : " + clientCert.GetPublicKeyString());
        Console.WriteLine("######## EQuals SERVER CERT : " + clientCert.Equals(certificate));

        // Force to return true
        return true;
    }
}

上述检查方法是否正确?如果不是为什么,可以解决什么问题。我也收到此警告“'System.Net.ServicePointManager.CertificatePolicy'已过时:'此类型的CertificatePolicy已废弃,请改用ServerCertificateValidationCallback。”。

有了这个,我怎么知道CheckValidationResult()是否返回false?

非常感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:2)

您是否尝试过使用过时消息中的ServerCertificateValidationCallback?例如,您可以将以下方法添加到现有的MyPolicy类中:

public bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    Console.WriteLine(sslPolicyErrors);  // Or whatever you want to do...
    return true;
}

完成后,您可以替换现有的

System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();

符合以下条件:

MyPolicy policy = new MyPolicy();
System.Net.ServicePointManager.ServerCertificateValidationCallback = policy.ValidateServerCertificate;

答案 1 :(得分:1)