一.SOAPHEADER
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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.} . |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步