WCF分布式开发必备知识系列文章4:Web Service(3)
用户可以访问数据库进行用户和密码的验证,这里为了说明用户密码的校验,仅仅作了简单的比较,不作实现.
再次定义Web Service服务类.代码如下,添加刚才实现的UserValidation类库.要在服务里使用.代码如下:
using System;
2using System.Web;
3using System.Web.Services;
4using System.Web.Services.Protocols;
5using WebServiceUserValidation;
6//定义WebService的命名空间,为避免重复,通常设置为URL地址
7[WebService(Namespace = "http://www.cnblogs.com/frank_xl/")]
8//Coded By Frank Xu Lei,2/17/2009
9[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
10public class FrankXuWebService : System.Web.Services.WebService
11{
12 //存储包含用户票据Soap Header信息的属性字段,MySoapHeader继承自SoapHeader,我们在服务端定义实现
13 public MySoapHeader _authenticationToken;
14 private const string _token = "FrankXuLei";//长属性字段,存储我们服务器端的票据,
15 //
16 public FrankXuWebService()
17 {
18
19 //Uncomment the following line if using designed components
20 //InitializeComponent();
21 }
22 //定义SoapHeader传递的方向,
23 //SoapHeaderDirection.In;只发送SoapHeader到服务端
24 //SoapHeaderDirection.Out;只发送SoapHeader到客户端
25 //SoapHeaderDirection.InOut;发送SoapHeader到服务端和客户端
26 //SoapHeaderDirection.Fault;服务端方法异常的话,会发送异常信息到客户端
27 [SoapHeader("_authenticationToken")]
28 //设置方法SoapHeader属性,用于WebService的方法处理Soap Header消息
29 //"_authenticationToken"就是定义的FrankXuWebService类的私有属性名称,
30 [WebMethod(EnableSession=false)]
31 public string HelloFrank()
32 {
33
34 //要想在浏览器直接使用Invoke测试服务,这个用户票据代码要注销,不然会无法调用
35 if (_authenticationToken != null && UserValidation.IsUserLegal(_authenticationToken.Token ))//验证票据的值是否正确,
36 {
37 return "Hello Frank,WebMethod is called sucessfully";
38 }
39 else
40 {
41 throw new Exception("Authentication Failed");
42 }
43 }
44
45}
定义SoapHeader传递的方向,SoapHeaderDirection.In;只发送SoapHeader到服务端,SoapHeaderDirection.Out;只发送SoapHeader到客户端,SoapHeaderDirection.InOut;发送SoapHeader到服务端和客户端SoapHeaderDirection.Fault;服务端方法异常的话,会发送异常信息到客户端.
最后建立控制台客户端,添加本地项目的web service引用,会生成一个代理类.这个负责和服务器段的交互.具体代码:
namespace ConsoleWebServiceClient
3{
4 class Program
5 {
6 static void Main(string[] args)
7 {
8
9 //实例化一个简单对象访问协议的头,SoapHeader
10 localhost.MySoapHeader mySoapHeader = new ConsoleWebServiceClient.localhost.MySoapHeader();
11 //对象设置客户端知道的票据的值
12 mySoapHeader.Token = "FrankXuLei";
13 string sResult = string.Empty;
14 localhost.FrankXuWebService frankXuWebService = null;
15 try
16 {
17 //实例化一个客户端引用Web服务的类
18 frankXuWebService = new ConsoleWebServiceClient.localhost.FrankXuWebService();
19 //设置Web服务的SoapHeader
20 frankXuWebService.MySoapHeaderValue = mySoapHeader;
21 //调用Web服务的HelloWorld()方法。
22 sResult = frankXuWebService.HelloFrank();
23 //输出结果
24 Console.WriteLine(sResult);
25 }
26 catch (Exception ex)
27 {
28 //
29 Console.WriteLine("Call WebService is failed");
30 throw ex;
31 }
32 finally
33 {
34 //释放托管资源
35 if (frankXuWebService != null)
36 frankXuWebService.Dispose();
37 }
38
39 //调试
40 Console.WriteLine("Press any key to continue");
41 Console.ReadLine();
42
43 }
44 }
45}
实例化一个客户端引用Web服务的代理类, frankXuWebService = new ConsoleWebServiceClient.localhost.FrankXuWebService();设置Web服务的SoapHeader frankXuWebService.MySoapHeaderValue = mySoapHeader;调用Web服务的HelloWorld()方法。 sResult = frankXuWebService.HelloFrank();输出结果:如下图:
上图显示了调用结果的成功,当客户端传递的票据正确的时候,服务方法会正确执行.返回客户端结果.否则执行将失败.
6.总结.
本文基本上介绍了Web Service的基本概念\优势和缺点\使用Web Service的场合\安全问题\实现代码部分,如何创建一个web服务和使用简单的认证机制SoapHeader来获取和验证用户请求的合法性.这个只是解决服务器端验证的问题.但是没有实现数据传递的加密.用户的密码信息的泄露也是Web Service的安全隐患.另外使用SSL(是Secure Sockets Layer通讯协议)用来保护传输中的资料,把在网页以及服务器之间的数据传输加密起来,在利用iis的安全认证机制,结合几重措施,才能很好地保护Web Service的安全.本节的代码下载/Files/frank_xl/WebService.rar.WSE也可以实现Web Service的通信安全.由于其涉及知识交多,篇幅限制不在这里多详细叙述.网上的资料不多,大部分是英文,我打算专门补充一个WSE安全开发系列文章,与大家进行学习.有兴趣的朋友可以继续关注.
另外本文也是这个系列的最后一节,基本上对之前的分布式开发的几个技术都做了回顾.这个也是对我们学习WCF分布式开发打了一个很好的基础.开发的很多知识都有相似之处.我们博学才能融会贯通.才能更好地学习WCF.希望此系列文章对大家有所帮助.谢谢!~~
(参考文章:http://baike.baidu.com/view/837392.htm)
WCF分布式开发必备知识系列文章快速学习列表:
WCF分布式开发必备知识系列文章1:MSMQ消息队列
http://www.xueit.com/html/2009-02/21_625_00.html
WCF分布式开发必备知识系列文章2:Net Remoting
http://www.xueit.com/html/2009-02/21_626_00.html
WCF分布式开发必备知识系列文章3:Enterpise Services
http://www.xueit.com/html/2009-02/21_627_00.html
WCF分布式开发必备知识系列文章4:Web Service
http://www.xueit.com/html/2009-02/21_628_00.html