1 [WebService(Namespace = "http://tempuri.org/")] 2 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 3 //[System.ComponentModel.ToolboxItem(false)] 4 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 5 //[System.Web.Script.Services.ScriptService] 6 public class WebService1 : System.Web.Services.WebService 7 { 8 public MySoapHeader soapHeader = new MySoapHeader(); 9 10 11 [MyExtension] 12 [SoapHeader("soapHeader")] 13 public string HelloWorld() 14 { 15 return "duile"; 16 } 17 } 18 19 public class MySoapHeader : SoapHeader 20 { 21 public string Pwd { get; set; } 22 public string Act { get; set; } 23 } 24 25 [AttributeUsage(AttributeTargets.Method)] 26 public class MyExtensionAttribute : SoapExtensionAttribute 27 { 28 int _priority = 1; 29 30 public override int Priority 31 { 32 get { return _priority; } 33 set { _priority = value; } 34 } 35 36 public override Type ExtensionType 37 { 38 get { return typeof(MyExtension); } 39 } 40 } 41 42 public class MyExtension : SoapExtension 43 { 44 45 public override void Initialize(object initializer) 46 { 47 string a = "b"; 48 } 49 //这个override的方法会被调用四次 50 //分别是SoapMessageStage的BeforeSerialize,AfterSerialize,BeforeDeserialize,AfterDeserialize 51 public override void ProcessMessage(SoapMessage message) 52 { 53 if (message.Stage == SoapMessageStage.AfterDeserialize)//反序列化之后处理 54 { 55 bool check = false; 56 foreach (SoapHeader header in message.Headers) 57 { 58 if (header is MySoapHeader) 59 { 60 MySoapHeader myHeader = (MySoapHeader)header; 61 //解密 62 63 if (myHeader.Pwd == "huwei" || myHeader.Act == "12456") 64 { 65 check = true; 66 break; 67 } 68 } 69 } 70 if (!check) 71 throw new SoapHeaderException("认证失败", SoapException.ClientFaultCode); 72 } 73 } 74 public override Object GetInitializer(Type type) 75 { 76 return GetType(); 77 } 78 public override Object GetInitializer(LogicalMethodInfo info, SoapExtensionAttribute attribute) 79 { 80 return null; 81 } 82 }
客户端调用
WebService1 service = new WebService1();
Response.Write("<script>alert('" + service.HelloWorld() + "')</script>");
为什么这样还可以直接调用成功呢?