服务端Web Service新建klmywgSoapHeader.cs
using System; using System.Data; using System.Configuration; using System.Runtime.InteropServices; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.Services; using System.Web.Services.Protocols; /// <summary> ///klmywgSoapHeader 的摘要说明 /// </summary> public class klmywgSoapHeader:SoapHeader { public string UserName; public string PassWord; private string m_UserName; private string m_PassWord; [DllImport("DataEncrypt.dll")] public static extern string DESEncrypt(string Value, string Key); [DllImport("DataEncrypt.dll")] public static extern string DESUnEncrypt(string Value, string Key); string key; public klmywgSoapHeader() { key = ConfigurationManager.AppSettings["key"].ToString(); m_UserName = ConfigurationManager.AppSettings["WebServiceUsername"].ToString(); m_PassWord = ConfigurationManager.AppSettings["WebServicePassword"].ToString(); m_PassWord = DESUnEncrypt(m_PassWord, key); } public string Validate() { PassWord = DESUnEncrypt(PassWord,key); if (m_UserName == UserName && m_PassWord == PassWord) { return "true"; } else { return "do not have permission"; } } }
WebService.cs
using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class Service : System.Web.Services.WebService { public klmywgSoapHeader header = new klmywgSoapHeader(); public Service () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] [SoapHeader("header")] public string HelloWorld() { string result= header.Validate(); if (result == "true") { return "Hello World"; } else return result; } }
网页调用
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class _Default : System.Web.UI.Page { localhost.klmywgSoapHeader header = new localhost.klmywgSoapHeader(); localhost.Service service = new localhost.Service(); protected void Page_Load(object sender, EventArgs e) { header.UserName = ConfigurationSettings.AppSettings["WebServiceUsername"].ToString(); header.PassWord = ConfigurationSettings.AppSettings["WebServicePassword"].ToString(); service.klmywgSoapHeaderValue = header; string result = service.HelloWorld(); Response.Write(result); } }