首页 > 开发 > 综合 > 正文

zt:Consuming Webservices over HTTPS (SSL)

2024-07-21 02:25:07
字体:
来源:转载
供稿:网友
when webservices are used, a common concern is security: soap messages are transferred in plain text over the network, so anyone with a sniffer could intercept the soap message and read it. in my opinion this could happen also to binary data, but probably it requires a little bit more hacker skills. so a solution is to use https (ssl) instead of http, so the communication is encrypted. to accomplish this, you need to get and install a certificate (issued by a certificate authority) on your webserver. in a production environment you would buy a certificate from verisign or another well known ca, or you would install your own ca, which is a component of windows server. if you only want to play with https, ssl and certificates or your project is in the development phase, you can also generate a test certificate using the makecert.exe tool (included in the .net framework sdk). after that you have to add this certificate to a website in iis, and set a port which https should use.

when you browse to a https site, you probably get a dialog window asking you if you want to trust the certificate provided by the webserver. so the responsibility of accepting the certificate is handled by the user. let's get back to the webservice scenario, if you want to invoke a webservice located on a webserver which uses ssl and https there is a problem. when you make the call from code, there is no dialog window popping up, and asking if you trust the certificate (luckily because this would be pretty ugly in server-side scenarios); probably you'll get following exception:
an unhandled exception of type 'system.net.webexception' occurred in system.dll

additional information: the underlying connection was closed: could not establish trust relationship with remote server.

but there is a solution for this problem, you can solve this in your code by creating your own certificatepolicy class (which implements the icertificatepolicy interface). in this class you will have to write your own checkvalidationresult function that has to return true or false, like you would press yes or no in the dialog window. for development purposes i've created the following class which accepts all certificates, so you won't get the nasty webexception anymore:
public class trustallcertificatepolicy : system.net.icertificatepolicy
{
public trustallcertificatepolicy()
{}

public bool checkvalidationresult(servicepoint sp,
x509certificate cert,webrequest req, int problem)
{
return true;
}
}

as you can see the checkvalidationresult function always returns true, so all certificates will be trusted. if you want to make this class a little bit more secure, you can add additional checks using the x509certificate parameter for example. to use this certificatepolicy, you'll have to tell the servicepointmanager to use it:
system.net.servicepointmanager.certificatepolicy = new trustallcertificatepolicy();
this must be done (one time during the application life cycle) before making the call to your webservice.
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表