Custom SOAP Message Header

Source: Internet
Author: User

For WebService calls, to verify the identity of the caller, you can customize a SoapHeader so that the caller can put the identity information in it and then check it on the server. The specific method is as follows:

1. First define a SoapHeader and use it to pass the identity information:

 
 
  1. Using System;
  2. Using System. Web. Services. Protocols;
  3.  
  4. Namespace CustomSoap
  5. {
  6. /// <Summary>
  7. /// Customize the SOAP header, derived from SoapHeader
  8. /// </Summary>
  9. Public class ServiceHeader: SoapHeader
  10. {
  11. /// <Summary>
  12. /// Define the username field
  13. /// </Summary>
  14. Public string Name {get; set ;}
  15. /// <Summary>
  16. /// Define the password field
  17. /// </Summary>
  18. Public string Pass {get; set ;}
  19. }
  20. }


2. Modify the service method in WebService:

 
 
  1. Using System;
  2. Using System. Web. Services;
  3. Using System. Web. Services. Protocols;
  4.  
  5. Namespace CustomSoap
  6. {
  7. [WebService (Namespace = "CustomSoap. Test")]
  8. [WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
  9. [System. ComponentModel. ToolboxItem (false)]
  10. Public class Service: System. Web. Services. WebService
  11. {
  12. /// <Summary>
  13. /// Define a ServiceHeader object
  14. /// </Summary>
  15. Public ServiceHeader Header {get; set ;}
  16.  
  17. /// <Summary>
  18. /// Service method. Use SoapHeader to mark which Header to use. Here is the Header attribute defined above.
  19. /// </Summary>
  20. /// <Returns> </returns>
  21. [WebMethod]
  22. [SoapHeader ("Header")]
  23. Public string Hello ()
  24. {
  25. String user = this. Header. Name;
  26. String pass = this. Header. Pass;
  27.  
  28. // You can determine your identity here. the user name and password are dead.
  29. If (string. Equals (user, "root") & string. Equals (pass, "pass "))
  30. Return "Hello root ";
  31. Else
  32. Return "Login Required ";
  33. }
  34. }
  35. }


3. The caller must pass the identity information:

 
 
  1. Public string CallHello ()
  2. {
  3. // ServiceProxy is the proxy class generated for Service. asmx.
  4. Var proxy = new CustomSoap. Remote. ServiceProxy ();
  5.  
  6. // Pass the identity information
  7. Proxy. ServiceHeaderValue = new CustomSoap. Remote. ServiceHeader ();
  8. Proxy. ServiceHeaderValue. Name = "root ";
  9. Proxy. ServiceHeaderValue. Pass = "pass ";
  10.  
  11. // Call the Remote Method
  12. Return proxy. Hello ();
  13. }

You can call this operation to receive "Hello root". If the user name or password is incorrect, you will receive "Login Required ".


At this time, the SOAP content will change. Capture the package or directly access Service. asmx in the browser? Op = Hello, you can see the request package:

 
 
  1. POST/Service. asmx HTTP/1.1
  2. Host: localhost
  3. Content-Type: text/xml; charset = UTF-8
  4. Content-Length: length
  5. SOAPAction: "CustomSoap. Test/Hello"
  6.  
  7. <? Xml version = "1.0" encoding = "UTF-8"?>
  8. <Soap: Envelope xmlns: xsi = "The http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "The http://www.w3.org/2001/XMLSchema" xmlns: soap = "The http://schemas.xmlsoap.org/soap/envelope/">
  9. <! -- The Header is added here, And the content is our custom ServiceHeader -->
  10. <Soap: Header>
  11. <ServiceHeader xmlns = "CustomSoap. Test">
  12. <Name> string </Name>
  13. <Pass> string </Pass>
  14. </ServiceHeader>
  15. </Soap: Header>
  16. <! -- END -->
  17. <Soap: Body>
  18. <Hello xmlns = "CustomSoap. Test"/>
  19. </Soap: Body>
  20. </Soap: Envelope>


In addition, for WebService, it is plain text SOAP communication, so you need to consider the solution for security.

 

This article from the rabbit nest blog, please be sure to keep this source http://boytnt.blog.51cto.com/966121/837345

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.