Two Methods for Web Service authentication

Source: Internet
Author: User

Web Service authentication solution 1: authentication by using the SOAP Header.

1. We implement a class for identity authentication, with the file name MySoapHeader. cs

The MySoapHeader class inherits from System. Web. Services. Protocols. SoapHeader. Two member variables, UserName and PassWord, are defined, and a user-authenticated function ValideUser is defined. It provides the UserName and PassWord check function.

  1. 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. Using System. Web. Services;
  11. Using System. Web. Services. Protocols;
  12. ///
  13. /// Summary of MySoapHeader
  14. ///
  15. Public class MySoapHeader: SoapHeader
  16. {
  17. Public MySoapHeader ()
  18. {
  19. //
  20. // TODO: add the constructor logic here
  21. //
  22. }
  23. Public string UserName;
  24. Public string PassWord;
  25. Public bool ValideUser (string in_UserName, string in_PassWord)
  26. {
  27. If (in_UserName = "zxq") & (in_PassWord = "123456 "))
  28. {
  29. Return true;
  30. }
  31. Else
  32. {
  33. Return false;
  34. }
  35. }
  36. }

2. The following Code creates WebService. asmx WebService. cs:

  1. Using System;
  2. Using System. Collections;
  3. Using System. Web;
  4. Using System. Web. Services;
  5. Using System. Web. Services. Protocols;
  6.  
  7. ///
  8. /// Summary of WebService
  9. ///
  10. [WebService (Namespace = "http://tempuri.org/")]
  11. [WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
  12. Public class WebService: System. Web. Services. WebService
  13. {
  14.  
  15. Public WebService ()
  16. {
  17.  
  18. // If you use the designed component, uncomment the following line
  19. // InitializeComponent ();
  20. }
  21. Public MySoapHeader header; // defines the user authentication class variable header
  22. [WebMethod (Description = "user verification test")]
  23. [System. Web. Services. Protocols. SoapHeader ("header")] // soap header for User Authentication
  24. Public string HelloWorld (string contents)
  25. {
  26. // Verify the access permission
  27. If (header. ValideUser (header. UserName, header. PassWord ))
  28. {
  29. Return contents + "executed ";
  30. }
  31. Else
  32. {
  33. Return "You are not authorized to access ";
  34. }
  35. }
  36. }

3. Create a Default. aspx Client

  1. Default. aspx. cs code
  2.  
  3. Using System;
  4. Using System. Configuration;
  5. Using System. Data;
  6. Using System. Web;
  7. Using System. Web. Security;
  8. Using System. Web. UI;
  9. Using System. Web. UI. HtmlControls;
  10. Using System. Web. UI. WebControls;
  11. Using System. Web. UI. WebControls. WebParts;
  12.  
  13. Public partial class _ Default: System. Web. UI. Page
  14. {
  15. Protected void Page_Load (object sender, EventArgs e)
  16. {
  17. Com. cn1yw. WebService test = new com. cn1yw. WebService (); // web reference (changed to your own)
  18. Com. cn1yw. MySoapHeader Header = new com. cn1yw. MySoapHeader (); // create a soap Header object by referencing the web (change it to your own)
  19. // Set the soap header variable
  20. Header. UserName = "zxq ";
  21. Header. PassWord = "123456 ";
  22. Test. MySoapHeaderValue = Header;
  23. // Call the web method
  24. Response. Write (test. HelloWorld ("I am strong "));
  25. }
  26. }

Web Service authentication solution 2: windows authentication is integrated.

1. Set the web service program to integrated windows Authentication

 

2. Client web reference code

  1. Test. WebReference. Service1 wr = new Test. WebReference. Service1 (); // generate a web service instance
  2. Wr. Credentials = new NetworkCredential ("guest", "123"); // guest is the user name, which requires certain permissions.
  3. LblTest. Text = wr. Add (2, 2). ToString (); // call the web service Method

The advantage of this solution is that it is relatively safe and has good performance. The disadvantage is that it is not easy to transplant and the deployment workload is large.

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.