一.SOAPHEADER
01.using System; 02.using System.Data; 03.using System.Configuration; 04.using System.Web; 05.using System.Web.Security; 06.using System.Web.UI; 07.using System.Web.UI.HtmlControls; 08.using System.Web.UI.WebControls; 09.using System.Web.UI.WebControls.WebParts; 10. 11./// <summary> 12.///MySoapHeader 的摘要说明 13./// </summary> 14.public class MySoapHeader:System .Web .Services .Protocols .SoapHeader 15.{ 16. private string _uname = string.Empty;//webservice访问用户名 17. 18. public string Uname 19. { 20. get { return _uname; } 21. set { _uname = value; } 22. } 23. private string _password = string.Empty;//webservice访问密码 24. 25. public string Password 26. { 27. get { return _password; } 28. set { _password = value; } 29. } 30. 31. 32. public MySoapHeader() 33. { 34. // 35. //TODO: 在此处添加构造函数逻辑 36. // 37. } 38. public MySoapHeader(string uname, string upass) 39. { 40. init(uname, upass); 41. } 42. private void init(string uname, string upass) 43. { 44. this._password = upass; 45. this._uname = uname; 46. } 47. //验证用户是否有权访问内部接口 48. private bool isValid(string uname, string upass, out string msg) 49. { 50. msg = ""; 51. if (uname == "admin" && upass =="admin") 52. { 53. return true; 54. } 55. else { 56. msg = "对不起!您无权调用此WebService!"; 57. return false; 58. } 59. } 60. //验证用户是否有权访问外部接口 61. public bool isValid(out string msg) 62. { 63. return isValid(_uname, _password,out msg); 64. } 65.} .
二.WEBSERVICE
01.using System; 02.using System.Collections; 03.using System.Linq; 04.using System.Web; 05.using System.Web.Services; 06.using System.Web.Services.Protocols; 07.using System.Xml.Linq; 08. 09./// <summary> 10.///test 的摘要说明 11./// </summary> 12.[WebService(Namespace = "http://tempuri.org/")] 13.[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 14.//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 15.// [System.Web.Script.Services.ScriptService] 16.public class test : System.Web.Services.WebService { 17. 18. public test () { 19. 20. //如果使用设计的组件,请取消注释以下行 21. //InitializeComponent(); 22. } 23. public MySoapHeader myheader = new MySoapHeader(); 24. [WebMethod] 25. public string HelloWorld() {//普通WebService,无需验证 26. return "Hello World"; 27. } 28. [SoapHeader("myheader")]//加入此头部的WebService需要验证,不加则为普通WebService无需验证 29. 30. [WebMethod(Description = "根据产品编号查询产品的价格", EnableSession = true)] 31. 32. public string GetProductPrice2(string ProductId) 33. { 34. 35. string msg = ""; 36. 37. //验证是否有权访问 38. 39. if (!myheader.isValid(out msg)) 40. { 41. 42. return -1;//返回错误信息 43. 44. } 45. 46. return ProductId; 47. } 48. } .
三.WEB
01.using System; 02.using System.Configuration; 03.using System.Data; 04.using System.Web; 05.using System.Web.Security; 06.using System.Web.UI; 07.using System.Web.UI.HtmlControls; 08.using System.Web.UI.WebControls; 09.using System.Web.UI.WebControls.WebParts; 10.public partial class _Default : System.Web.UI.Page 11.{ 12. protected void Page_Load(object sender, EventArgs e) 13. { 14. myservice.test te = new abc.test(); 15. myservice.MySoapHeader myhead = new MySoapHeader(); 16. myhead.Uname = "admin";//输入WebService访问用户名 17. myhead.Password = "admin";//输入WebService访问密码 18. te.MySoapHeaderValue = myhead;// 19. string test = te.GetProductPrice2("ok!"); 20. Response.Write(aa);//用户名、密码输入正确则输出ok 否则输出 错误msg 21. } 22.} .