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

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

wKioL1l8JUCCC3g2AAB-mIsIdYA840.png-wh_50

我的WebService的结构如下图:

wKiom1l8Jd2z2RTJAABcrpcm_2k524.png-wh_50

好了,这里主要讲解身份验证类以及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
{
    /// <summary>
    /// WebAiny 的摘要说明
    /// </summary>
    [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服务器配置,读者可以搜百度自己解决。运行程序如下:

wKioL1l8J__Doip0AADrjhYf7d8326.png-wh_50

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

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


①,引入WebService

wKioL1l8KFqCQKs_AAEhYE7B24o096.png-wh_50

wKiom1l8KFvzccVVAAC1w4nqs_A901.png-wh_50

wKiom1l8KFyTUfm1AAE76HLzTpw585.png-wh_50

wKioL1l8KFyyk8hSAAHys3Yc6e0284.png-wh_50

如上图,我的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();
        }
    }
}

结果:

wKioL1l8KOvSk7hKAABRgyqQOcE812.png-wh_50

需要指出的是 : 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();
        }
    }
}

结果为:

wKioL1l8KZfAhLYTAAA6ZiWedqc015.png-wh_50

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