您的位置:首页 > 其它

实现带有用户身份验证的Web Service

2007-05-08 13:52 267 查看
一、 理解Web Service

  首先让我们简单理解一下Web Service。

  Web services 实际上是基于XML的针对商业、应用程序的系统服务。实际上是建立在一系列已有的技术基础上的一个新的概念。使用它如同使用RPC(远程过程调用),不过它提供的接口是基于对象的。它与原有的组件模型,如com/dcom,corba,rmi等,最大的特点就是标准化(基于XML的一系列标准)带来的跨平台的通用性,基于http带来的畅通无阻的能力(跨越防火墙),对于Dot Net来说,配合Visual Studio.Net其中的Web Service还具有易用性的特点。用户即便不懂那些相关标准,只要会面向对象变成,对Web Service有个初步的了解就可以动手写Web Service了。

  如何建立一个Web service呢?下面的技术步骤获得了工业界普遍认同。

  1.服务提供者建立、组织和发布网络服务。它的手段是多样的,可以使用编程语言(例如Delphi,Java,C#等)、中间件或其它特殊平台来完成。

  2.服务提供者用WSDL (the Web Services Description Language )来提供服务描述。. WSDL文档向第三方提供服务描述。

  3.服务提供者向UDDI (Universal Description, Discovery, and Integration ) 注册表注册网络服务。UDDI使得开发者发布服务并且可以通过软件搜索其它人提供的服务。 用户将通过搜索UDDI 注册表来搜索服务。

  4. 客户端应用程序通过和网络服务绑定并且通过SOAP (the Simple Object Access Protocol )来激活网络服务相应的操作。SOAP 提供XML格式的参数和返回结果,并利用HTTP传送。所有网络服务都通过SOAP通讯。

  上面的第一步是核心,它决定了你的Web Service的用途和接口。第二步在Visual Studio.Net中被自动完成,大家当然也可以用WSDL.exe手动生成。第四步在客户端编程时Visual Studio提供了最大的便利。用户只要使用” Add Web Service Reference”或者“添加Web服务引用”(中文版),敲入网址,Visual Studio.Net就会自动生成调用Web Service的类,底层的SOAP对用户完全透明。

  但是,当用户需要考虑安全性时就不得不考虑一下soap了。记住,安全性是将来必须考虑的,在将来软件开发变为配置集成,软件产品变为服务,购买软件变为租用软件,大部分服务都基于网络时,你可能得考虑谁可以用你的服务,谁不可以用你的服务了,当然,我也比较赞同开放源代码和公开交流技术,我把自己辛苦钻研出来的东西写出来也表明了我的态度。但是,毕竟,我们还是要靠写软件吃饭的:)

  在web service中,用户名,密码可以通过soap头(soap header)进行传送。在web service的编写中,需要对soap头进行处理,不过这也不太难,大家认识到这一点然后看我下面的例子就可以学会如何利用它了。

二、 代码实现

  首先,我们实现一个用于身份验证的类,文件名Authentication.cs为代码如下:

using System;

namespace useResData

{

 ///

 ///实现带有用户身份验证的文件传输Web Service

 ///

 public class Authentication: System.Web.Services.Protocols.SoapHeader

 {

  public string Username;

  public string Password;

  public Boolean ValidUser(string in_Username,

  string in_Password)

  {

   if((in_Username == "caomo") && (in_Password == "password"))

    {

     return true;

    }

   else

   {

    return false;

   }

  }

  public Authentication()

  {

  //

  // TODO: Add constructor logic here

  //

  }

 }

}

  Authentication类继承自System.Web.Services.Protocols.SoapHeader。且定义了两个成员变量,Username和Password,还定义了一个用户认证的函数ValidUser。它提供了对Username和Password检查的功能,你可以把它写得很复杂,诸如通过访问数据库来检查用户的有效性。我在这里只是简单检查了固定值的相等性。

下面我们生成一个web service,起名叫FileServer,在FileServer.asmx中有如下代码:

<%@ WebService Language="c#" Codebehind="FileServer.asmx.cs" Class="useResData.FileServer" %>

  大家可以看到Codebehind技术是如何被使用的。在Visual Studio.Net中,自动生成的代码大量使用这样的语句。它使得设计页面和编写代码被划分开了。

  在FileServer.asmx.cs中,代码如下:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Web;

using System.Web.Services;

using System.IO;

namespace useResData

{

 ///

 /// Summary description for FileServer.

 ///

 public class FileServer : System.Web.Services.WebService

 {private string rootdir;

  public FileServer()

  {

  //CODEGEN: This call is required by the ASP.NET Web Services Designer

  InitializeComponent();

  rootdir=Server.MapPath("/caomo/提供传输的文件");

  }

  #region Component Designer generated code

  ///

  /// Required method for Designer support - do not modify

  /// the contents of this method with the code editor.

  ///

  private void InitializeComponent()

  {

  }

  #endregion

  ///

  /// Clean up any resources being used.

  ///

 protected override void Dispose( bool disposing )
 
 {

 }

 public Authentication header; //定义用户身份验证类变量header。

  [WebMethod(Description="need authentication!")]

  [System.Web.Services.Protocols.SoapHeader("header")]
   //用户身份验证的soap头

 public string GetFile(string filePath)

 {

  if (header.ValidUser(header.Username,header.Password)) //用户身份验证

  {

   FileStream myfile=File.OpenRead(rootdir+filePath);

   BinaryReader br=new BinaryReader(myfile);

   byte[] btBuf=new byte[myfile.Length];

   long i=0;

   while (br.PeekChar()>-1)

   {

    btBuf[i]=br.ReadByte();

    i++;

   }

   myfile.Close();

   return System.Convert.ToBase64String(btBuf);

  }

  else return null;//用户身份验证failed

 }

  运行它。将会得到如图1所示页面:

图 1

  大家应该注意到名为GetFile的服务是我给的代码中的Web Method,下面的“need authentication!”是由WebMethod定义中的Description="need authentication!"给出的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: