欢迎光临
我们一直在努力

实现带有用户身份验证的文件传输Web Service(3) (转)-.NET教程,Web Service开发

建站超值云服务器,限时71元/月

作者: 曹勇刚   www.aspcool.com 时间:2001-11-28 22:51:59  阅读次数:493

     下面我们生成一个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!"给出的。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 实现带有用户身份验证的文件传输Web Service(3) (转)-.NET教程,Web Service开发
分享到: 更多 (0)