none
ASMX Web Service Soap Extension ProcessMessage function not calling RRS feed

  • Question

  • i have a asmx web service and i attached the Soap Extension just to intercept data and encrypt/decrypt it but the problem is Soap Extension ProcessMessage function not calling

    here is my full code with web.config and i am running the code from VS2013.

    please see my code and tell me where i made the mistake for which ProcessMessage function not calling

    namespace EncryptASMX
    {
        /// <summary>
        /// Summary description for Test1
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
        public class Test1 : System.Web.Services.WebService
        {
            public AuthenticateHeader Credentials;
    
            [AuthExtension]
            [SoapHeader("Credentials", Required = true)]
            [WebMethod]
            public string Add(int x, int y)
            {
                return (x + y).ToString();
            }
        }
    
        public class AuthenticateHeader : SoapHeader
        {
            public string UserName;
            public string Password;
        }
    
        [AttributeUsage(AttributeTargets.Method)]
        public class AuthExtensionAttribute : SoapExtensionAttribute
        {
            int _priority = 1;
    
            public override int Priority
            {
                get { return _priority; }
                set { _priority = value; }
            }
    
            public override Type ExtensionType
            {
                get { return typeof(AuthExtension); }
            }
        }
    
        public class AuthExtension : SoapExtension
        {
            public override void ProcessMessage(SoapMessage message)
            {
                if (message.Stage == SoapMessageStage.AfterDeserialize)
                {
                    //Check for an AuthHeader containing valid
                    //credentials
                    foreach (SoapHeader header in message.Headers)
                    {
                        if (header is AuthenticateHeader)
                        {
                            AuthenticateHeader credentials = (AuthenticateHeader)header;
                            if (credentials.UserName.ToLower() ==
                                "jeff" &&
                                credentials.Password.ToLower() ==
                                "imbatman")
                                return; // Allow call to execute
                            break;
                        }
                    }
    
                    // Fail the call if we get to here. Either the header
                    // isn't there or it contains invalid credentials.
                    throw new SoapException("Unauthorized",
                        SoapException.ClientFaultCode);
                }
            }
    
            public override Object GetInitializer(Type type)
            {
                return GetType();
            }
    
            public override Object GetInitializer(LogicalMethodInfo info,
                SoapExtensionAttribute attribute)
            {
                return null;
            }
    
            public override void Initialize(Object initializer)
            {
            }
        }
    }

    web.config entry

    <?xml version="1.0"?>
    
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    
    <configuration>
        <system.web>
          <compilation debug="true" targetFramework="4.5" />
          <httpRuntime targetFramework="4.5" />
    
          <webServices>
            <soapExtensionTypes>
              <add type="EncryptASMX.AuthExtension,EncryptASMX" priority="1"  />
            </soapExtensionTypes>
          </webServices>
    
        </system.web>
    
    </configuration>

    i consult two links but still not configure how to run process message. here are the links

    http://www.c-sharpcorner.com/article/using-soap-header-and-soap-extensions-in-a-web-service/

    http://www.codeguru.com/csharp/csharp/cs_webservices/security/article.php/c5479/Build-Secure-Web-Services-With-SOAP-Headers-and-Extensions.htm

    • Edited by Sudip_inn Monday, December 19, 2016 1:06 PM
    Monday, December 19, 2016 1:04 PM

Answers

  • Hi Mou_inn,

    How did you call Add method? Did you invoke the Add function in Web Browser or create a client? In my option, SOAP extensions are between a Web Service and Web Service client.

    For consuming SOAP Extension, I suggest you create a Web Service Client by Add Service Reference in VS->Advanced->Add Web Reference->Enter the Web Service URL and Add Reference. After you call Add method from Web Service Client, you will find the ProcessMessage is called.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, December 21, 2016 1:25 PM
    Tuesday, December 20, 2016 2:42 AM