WebService Access Encryption-soapheader

Source: Internet
Author: User

Go WebService head encryption, you canWebService set Access user name and password, enhance the security of WebService so that webservice can only be used by authorized users.
Specific implementation steps:
1. define a SoapHeader derived class to implement WebService access validation [CSharp]View Plaincopy
  1. <span style="font-size:10px;"  >using System;
  2. Using System.Data;
  3. Using System.Configuration;
  4. Using System.Web;
  5. Using System.Web.Security;
  6. Using System.Web.UI;
  7. Using System.Web.UI.HtmlControls;
  8. Using System.Web.UI.WebControls;
  9. Using System.Web.UI.WebControls.WebParts;
  10. <summary>
  11. Summary description of Mysoapheader
  12. </summary>
  13. Public class Mysoapheader:system. Web. Services. Protocols. SoapHeader
  14. {
  15. private String _uname = string. Empty; //webservice Access User name
  16. public string Uname
  17. {
  18. get { return _uname;}
  19. set {_uname = value;}
  20. }
  21. private String _password = string. Empty; //webservice access password
  22. public string Password
  23. {
  24. get { return _password;}
  25. set {_password = value;}
  26. }
  27. Public Mysoapheader ()
  28. {
  29. //  
  30. //todo: Add constructor logic here
  31. //  
  32. }
  33. Public Mysoapheader (string uname, string upass)
  34. {
  35. Init (uname, UPass);
  36. }
  37. private void init (string uname, string upass)
  38. {
  39. This._password = UPass;
  40. this._uname = uname;
  41. }
  42. //Verify that users have access to the internal interface
  43. Private bool IsValid (string uname, string upass, out string msg)
  44. {
  45. msg = "";
  46. if (uname = = "Admin" && upass = ="admin")
  47. {
  48. return true;
  49. }
  50. else {
  51. msg = "Sorry! You do not have permission to call this webservice!  ";
  52. return false;
  53. }
  54. }
  55. //Verify that users have access to external interfaces
  56. Public bool IsValid (out string msg)
  57. {
  58. return IsValid (_uname, _password, outmsg);
  59. }
  60. }
  61. </span>

2, define the need to verify the WebService.

[CSharp]View Plaincopy
  1. <span style="font-size:10px;"  >using System;
  2. Using System.Collections;
  3. Using System.Linq;
  4. Using System.Web;
  5. Using System.Web.Services;
  6. Using System.Web.Services.Protocols;
  7. Using System.Xml.Linq;
  8. <summary>
  9. Summary description of Test
  10. </summary>
  11. [WebService (Namespace = "http://tempuri.org/")]
  12. [WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
  13. To allow the Web service to be called from the script using ASP. NET AJAX, uncomment the downstream.
  14. [System.Web.Script.Services.ScriptService]
  15. Public class Test:System.Web.Services.WebService {
  16. Public Test () {
  17. //If you are using a design component, uncomment the following line
  18. //initializecomponent ();
  19. }
  20. Public Mysoapheader myheader = new Mysoapheader ();
  21. [WebMethod]
  22. public String HelloWorld () {//Normal WebService, no verification required
  23. return "Hello World";
  24. }
  25. [SoapHeader ("MyHeader")]//Join this header WebService need to verify, not add to the normal WebService no need to verify
  26. [WebMethod (Description = "The price of the product is queried according to the product number", EnableSession = true)]
  27. public string GetProductPrice2 (string ProductId)
  28. {
  29. string msg = "";
  30. //Verify that you have access to
  31. if (!myheader.isvalid (outmsg))
  32. {
  33. return-1; //Return error message
  34. }
  35. return ProductId;
  36. }
  37. }
  38. </span>

3. Client Call Method
The reference WebService defines the WebService name as : MyService

[CSharp]View Plaincopy
  1. <span style="font-size:10px;"  >using System;
  2. Using System.Configuration;
  3. Using System.Data;
  4. Using System.Web;
  5. Using System.Web.Security;
  6. Using System.Web.UI;
  7. Using System.Web.UI.HtmlControls;
  8. Using System.Web.UI.WebControls;
  9. 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"; Enter WebService Access user name
  17. Myhead. Password = "admin"; Enter WebService access password
  18. Te. Mysoapheadervalue = Myhead; //  
  19. string test = Te.  GetProductPrice2 ("ok!");
  20. Response.Write (AA); //username, password input correct output OK otherwise output error msg
  21. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.