身份验证类:(需要继承System.Web.Services.Protocols。SoapHeader)
using 系统; using System.Collections.Generic; using 来; using 包含; 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 价值;,} ,,,,,,} ,,,,,,public string UserPW ,,,,,,{ ,,,,,,,,,,get {userPW;, return } ,,,,,,,,,,set {=, userPW 价值;,} ,,,,,,} ,,,,,,public Authentication_WS () ,,,,,,{,} ,,,,,,public Authentication_WS(名称、string string 密码) ,,,,,,{ ,,,,,,,,,,userID =,名称; ,,,,,,,,,,userPW =,密码; ,,,,,,} ,,,,,,private bool IsValid (string nUserId, string nPassWord,, out string nMsg) ,,,,,,{ ,,,,,,,,,,nMsg =, "; ,,,,,,,,,,试一试 ,,,,,,,,,,{ ,,,,,,,,,,,,,,if (==nUserId “admin”,,,, nPassWord ==,“管理”) ,,,,,,,,,,,,,,{ ,,,,,,,,,,,,,,,,,,return 真实; ,,,,,,,,,,,,,,} ,,,,,,,,,,,,,, ,,,,,,,,,,,,,,{ ,,,,,,,,,,,,,,,,,,nMsg =,“对不起,,你have no right 用call 从而Web service ”; ,,,,,,,,,,,,,,,,,,return 假; ,,,,,,,,,,,,,,} ,,,,,,,,,,} ,,,,,,,,,, ,,,,,,,,,,{ ,,,,,,,,,,,,,,nMsg =,“对不起,,你have no right 用call 从而Web 服务”; ,,,,,,,,,,,,,,return 假; ,,,,,,,,,,} ,,,,,,} ,,,,,,public bool IsValid (out string  nMsg) ,,,,,,{ ,,,,,,,,,,return IsValid (userID, userPW out nMsg); ,,,,,,} ,,,} }
好了,加入身份验证也是为了让服务更加的安全。
在服务中使用身份验证信息:
using 系统; using System.Collections.Generic; using 来; using 包含; using System.Web.Script.Services; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Serialization; namespace WebEmpty { ,,,///,& lt; summary> ,,,///,WebAiny 的摘要说明 ,,,///,& lt;/summary> ,,,(网络服务(Namespace =, " http://tempuri.org/")) null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullc# WebService详解