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

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

我的WebService的结构如下图:

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

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

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Services.Protocols;namespaceWebEmpty{publicclassAuthentication_WS:SoapHeader{privatestringuserID=string.Empty;privatestringuserPW=string.Empty;publicstringUserId{get{returnuserID;}set{userID=value;}}publicstringUserPW{get{returnuserPW;}set{userPW=value;}}publicAuthentication_WS(){}publicAuthentication_WS(stringname,stringpassword){userID=name;userPW=password;}privateboolIsValid(stringnUserId,stringnPassWord,outstringnMsg){nMsg="";try{if(nUserId=="admin"&&nPassWord=="admin"){returntrue;}else{nMsg="Sorry,youhavenorighttocalltheWebservice";returnfalse;}}catch{nMsg="Sorry,youhavenorighttocalltheWebservice";returnfalse;}}publicboolIsValid(outstringnMsg){returnIsValid(userID,userPW,outnMsg);}}}

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

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

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Script.Services;usingSystem.Web.Services;usingSystem.Web.Services.Protocols;usingSystem.Xml.Serialization;namespaceWebEmpty{///<summary>///WebAiny的摘要说明///</summary>[WebService(Namespace="http://tempuri.org/")][WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)]//若要允许使用ASP.NETAJAX从脚本中调用此Web服务,请取消注释以下行。//[System.Web.Script.Services.ScriptService]//[XmlInclude(typeof(DM_Introduce))]publicclassWebAiny:System.Web.Services.WebService{publicAuthentication_WSauthentication=newAuthentication_WS();//引入身份验证类//[OperationContract]//[WebGet(UriTemplate="Add/{x}/{y}",ResponseFormat=WebMessageFormat.Xml)][WebMethod(Description="测试WebService")][ScriptMethod(ResponseFormat=ResponseFormat.Xml)][SoapHeader("authentication")]publicstringAdd(inta,intb){stringmsg="";if(!authentication.IsValid(outmsg)){returnmsg;}else{return(a+b).ToString();}}[WebMethod(Description="测试类型")][ScriptMethod(ResponseFormat=ResponseFormat.Xml)]publicvoidType(){Context.Response.Write("OK-noSOAPxmlData");}}}

(重点)注意点:

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


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

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

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


①,引入WebService

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

②看测试代码 :

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceClient2WebService{classProgram{staticvoidMain(string[]args){WS.WebAinywebAiny=newWS.WebAiny();WS.Authentication_WSauthentication=newWS.Authentication_WS();authentication.UserId="admin";authentication.UserPW="admin";webAiny.Authentication_WSValue=authentication;stringresult=webAiny.Add(1,3);Console.WriteLine("1+3={0}",result);Console.ReadLine();}}}

结果:

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

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

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceClient2WebService{classProgram{staticvoidMain(string[]args){WS.WebAinywebAiny=newWS.WebAiny();WS.Authentication_WSauthentication=newWS.Authentication_WS();authentication.UserId="admin";authentication.UserPW="admin1";webAiny.Authentication_WSValue=authentication;stringresult=webAiny.Add(1,3);Console.WriteLine("1+3={0}",result);Console.ReadLine();}}}

结果为:

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