Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2079993
  • 博文数量: 519
  • 博客积分: 10070
  • 博客等级: 上将
  • 技术积分: 3985
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-29 14:05
个人简介

只问耕耘

文章分类

全部博文(519)

文章存档

2016年(1)

2013年(5)

2011年(46)

2010年(220)

2009年(51)

2008年(39)

2007年(141)

2006年(16)

我的朋友

分类: WINDOWS

2011-07-07 15:17:19

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. lets 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 youll 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 ive created the following class which accepts all certificates, so you wont 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, youll 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.
阅读(1057) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~