资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#WebService详解

这里直接讲解WebService的实现过程及其中应该注意的点,有关其应用的环境等请度娘或者google。

在双流等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站制作、成都网站设计 网站设计制作定制开发,公司网站建设,企业网站建设,高端网站设计,全网整合营销推广,外贸营销网站建设,双流网站建设费用合理。

首先新建一个WebService服务:如图:

C# WebService详解

我的WebService的结构如下图:

C# WebService详解

好了,这里主要讲解身份验证类以及asmx服务使用身份验证应该注意的问题:

身份验证类:(需要继承System.Web.Services.Protocols.SoapHeader)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Protocols;
namespace WebEmpty
{
    public class Authentication_WS : SoapHeader
    {
       private string userID = string.Empty;
       private string userPW = string.Empty;
       public string UserId
       {
           get { return userID; }
           set { userID = value; }
       }
       public string UserPW
       {
           get { return userPW; }
           set { userPW = value; }
       }
       public Authentication_WS()
       { }
       public Authentication_WS(string name, string password)
       {
           userID = name;
           userPW = password;
       }
       private bool IsValid(string nUserId, string nPassWord, out string nMsg)
       {
           nMsg = "";
           try
           {
               if (nUserId == "admin" && nPassWord == "admin")
               {
                   return true;
               }
               else
               {
                   nMsg = "Sorry, you have no right to call the Web service ";
                   return false;
               }
           }
           catch
           {
               nMsg = "Sorry, you have no right to call the Web service";
               return false;
           }
       }
       public bool IsValid(out string nMsg)
       {
           return IsValid(userID,userPW,out nMsg);
       }
    }
}

好了 , 加入身份验证也是为了让服务更加的安全。

在服务中使用身份验证信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
namespace WebEmpty
{
    /// 
    /// WebAiny 的摘要说明
    /// 
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    //[System.Web.Script.Services.ScriptService]
    //[XmlInclude(typeof(DM_Introduce))]
    public class WebAiny : System.Web.Services.WebService
    {
        public Authentication_WS authentication = new Authentication_WS();//引入身份验证类
        //[OperationContract]
        //[WebGet(UriTemplate = "Add/{x}/{y}", ResponseFormat = WebMessageFormat.Xml)] 
        [WebMethod(Description="测试WebService")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        [SoapHeader("authentication")]
        public string Add(int a, int b)
        {
            string msg = "";
            if (!authentication.IsValid(out msg))
            {
                return msg;
            }
            else
            {
                return (a + b).ToString();
            }
        }
        [WebMethod(Description = "测试类型")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        public void Type() {
            Context.Response.Write("OK - no SOAP xml Data"); 
        }
    }
}

(重点)注意点:

①Add方法使用了身份验证功能 , 所以此方法上需要加一个特性:[SoapHeader("authentication")]   ( authentication -》 public Authentication_WS authentication = new Authentication_WS();//引入身份验证类)

这个IIS web服务器配置,读者可以搜百度自己解决。运行程序如下:

C# WebService详解

我建了一个控制台程序来测试这个WebService。

是使用WebService的功能必须要引用WebService的服务,引用方法步骤如下所示:


①,引入WebService

C# WebService详解

C# WebService详解

C# WebService详解

C# WebService详解

如上图,我的WebService的引用名称为WS

②看测试代码 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Client2WebService
{
    class Program
    {
        static void Main(string[] args)
        {
            WS.WebAiny webAiny = new WS.WebAiny();
            WS.Authentication_WS authentication = new WS.Authentication_WS();
            authentication.UserId = "admin";
            authentication.UserPW = "admin";
            webAiny.Authentication_WSValue = authentication;
            string result = webAiny.Add(1, 3);
            Console.WriteLine("1+3 = {0}", result);
            Console.ReadLine();
        }
    }
}

结果:

C# WebService详解

需要指出的是 : Authentication_WSValue属性是系统自动生成的(就是自己的身份验证类后面紧加一个Value),用于设置验证类。

我们给一个错误的账号(密码错误):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Client2WebService
{
    class Program
    {
        static void Main(string[] args)
        {
            WS.WebAiny webAiny = new WS.WebAiny();
            WS.Authentication_WS authentication = new WS.Authentication_WS();
            authentication.UserId = "admin";
            authentication.UserPW = "admin1";
            webAiny.Authentication_WSValue = authentication;
            string result = webAiny.Add(1, 3);
            Console.WriteLine("1+3 = {0}", result);
            Console.ReadLine();
        }
    }
}

结果为:

C# WebService详解

这样就能比较好的保护自己的服务了。。。。。。


分享题目:C#WebService详解
网站URL:http://cdkjz.cn/article/gpccjp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220