WebService access encryption and webservice Encryption

Source: Internet
Author: User

WebService access encryption and webservice Encryption
WebService encryption allows you to set the access username and password for WebService, enhancing the security of WebService so that WebService can only be used by authorized users.

Specific steps:

1. Define a soapheader derived class to verify WebService Access Permissions

  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 of MySoapHeader
  12. /// </Summary>
  13. Public class MySoapHeader: System. Web. Services. Protocols. SoapHeader
  14. {
  15. Private string _ uname = string. Empty; // webservice access Username
  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 the 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 whether the user has the right to access 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 whether the user has the right to access the external interface
  56. Public bool isValid (out string msg)
  57. {
  58. Return isValid (_ uname, _ password, out msg );
  59. }
  60. }
  61. </Span>


2. Define the WebService to be verified.

  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 of test
  10. /// </Summary>
  11. [WebService (Namespace = "http://tempuri.org/")]
  12. [WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
  13. // To allow ASP. net ajax to call this Web service from a script, cancel the comments to the downstream.
  14. // [System. Web. Script. Services. ScriptService]
  15. Public class test: System. Web. Services. WebService {
  16. Public test (){
  17. // If you use the designed component, uncomment the following line
  18. // InitializeComponent ();
  19. }
  20. Public MySoapHeader myheader = new MySoapHeader ();
  21. [WebMethod]
  22. Public string HelloWorld () {// common WebService, no verification required
  23. Return "Hello World ";
  24. }
  25. [SoapHeader ("myheader")] // the WebService with this header needs to be verified. If this header is not added, it is a common WebService and does not need to be verified.
  26. [WebMethod (Description = "querying product prices by product number", EnableSession = true)]
  27. Public string GetProductPrice2 (string ProductId)
  28. {
  29. String msg = "";
  30. // Verify the access permission
  31. If (! Myheader. isValid (out msg ))
  32. {
  33. Return-1; // return the error message
  34. }
  35. Return ProductId;
  36. }
  37. }
  38. </Span>



3. Client call Method

Reference WebService definition WebService name: Myservice

    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 the WebService access Username
    17. Myhead. Password = "admin"; // enter the WebService Access Password
    18. Te. MySoapHeaderValue = myhead ;//
    19. String test = te. GetProductPrice2 ("OK! ");
    20. Response. Write (aa); // if the user name and password are entered correctly, the output is OK; otherwise, the output is incorrect.
    21. }
    22. } </Span>

Related Article

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.