2010-07-13 48 views
0

我需要使用SSL牛逼建立到服务器的连接在C#winApplication使用SSL

描述: 我工作的一个Windows应用程序项目中,我们使用“Web服务”,以数据与数据库进行通信。 服务器端服务是用Java编写的,我正在用C#做UI部分。现在我想用SSL与服务器建立安全连接,但是我找不到一种方法来跳过生成的警告消息尝试建立连接时(使用C#代码)。

样本将帮助我很多,

这是我的代码使用方法:

地址:

public bool UserLogIn (User user) 
    { 
     logon logIn = new logon(); 

     logIn.arg0 = new userAthenticationRq(); 
     logIn.arg0.credential = GetCredential(user); 
     logIn.arg0.clientIpAddress = GetClientIP(); 
     logIn.arg0.requstDate = DateTime.Now; 
     logIn.arg0.requstDateSpecified = true; 
     logIn.arg0.userName = user.Name; 

     logonResponse rsp = new logonResponse(); 

     ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateRemoteCertificate); 

     rsp = service.logon(logIn);//this is where the Exception Occurs 


     if ([email protected] == statusCodeEnum.SUCCESS) 
     . 
     . 
     . 
    } 
+0

这是关于无效SSL证书的错误吗? – w69rdy 2010-07-13 10:51:49

+0

请参阅[如何使用c#2.0 WebClient忽略证书错误 - 无证书](http://stackoverflow.com/questions/1301127/how-to-ignore-a-certificate-error-with-c-2-0 -webclient-without-the-certificate) – 2010-07-13 10:52:21

+0

您不需要将您的电子邮件作为问题的一部分发布。我已经把它关闭了... – 2010-07-13 12:29:56

回答

0

如果其对无效的SSL证书错误您在使用本可以绕过它这对你的代码

// callback used to validate the certificate in an SSL conversation 
private static bool ValidateRemoteCertificate(object sender, X509Certificate certificate, 
    X509Chain chain, SslPolicyErrors policyErrors) 
{ 
     return true; 
} 

并添加此调用它并接受所有certi ficates:

// Adds validation of SSL Certificates 
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate); 
+0

感谢您的回答, 我使用了这段代码,但是当我尝试调用该服务时,我得到一个异常(WebException),出现如下错误消息,如 “请求失败,返回一个空的响应” 。 – shahab 2010-07-13 11:09:26

+0

你使用的是https还是http地址?看看这里http://bytes.com/topic/net/answers/826258-web-service-request-failed-empty-response – w69rdy 2010-07-13 11:43:33

+0

我正在使用Http ...当我改变它为Https它工作! 显然问题解决了。 非常感谢 – shahab 2010-07-13 12:21:54