none
How can i change the ws-security prefix RRS feed

  • Question

  • Hi,

    i need to change the prefix of my security node of my request header from <o:Security></o:Security> to <wsse:Security></wsse:Security>. i'm using C# WCF and consuming an external webservice. how can i achieve this?

    Thanks.

    Friday, August 31, 2018 4:33 PM

All replies

  • Hi Piptabor,

    According to your description, it seems that you want to customize the Soap message header. Generally speaking, you could use the message inspector to intercept the message by implementing the IDispatchMessageInspector and IClientMessageInspecto, and change it before sent it.

    Here is the official document.

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/message-inspectors

    I have made a demo wish it is useful to you.

    Server-side.

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                Uri uri = new Uri("http://localhost:1300");
    
                BasicHttpBinding binding = new BasicHttpBinding();
    
                using (ServiceHost sh = new ServiceHost(typeof(MyService), uri))
    
                {
    
                    ServiceMetadataBehavior smb;
    
                    smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
    
                    if (smb==null)
    
                    {
    
                        smb = new ServiceMetadataBehavior()
    
                        {
    
                            HttpGetEnabled = true
    
                        };
    
                        sh.Description.Behaviors.Add(smb);
    
                    }
    
                    sh.Open();
    
                    Console.WriteLine("service is ready...");
    
                    Console.ReadLine();
    
                    sh.Close();
    
                }
    
               
    
            }
    
        }
    
        [ServiceContract(Namespace ="mydomain")]
    
        [CustomContractBehavior]
    
        public interface IService
    
        {
    
            [OperationContract]
    
            string SayHello();
    
        }
    
        public class MyService : IService
    
        {
    
            public string SayHello()
    
            {
    
                return "Hello Stranger";
    
            }
    
        }
    
        public class ServerMessageLogger : IDispatchMessageInspector
    
        {
    
            public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    
            {
    
                string displayText = $"Server has received the following message:\n{request}\n";
    
                Console.WriteLine(displayText);
    
                return null;
    
            }
    
     
    
            public void BeforeSendReply(ref Message reply, object correlationState)
    
            {
    
                string displayText = $"Server has replied the following message:\n{reply}\n";
    
                Console.WriteLine(displayText);
    
               
    
            }
    
        }
    
        public class CustomContractBehaviorAttribute : Attribute, IContractBehavior,IContractBehaviorAttribute
    
        {
    
            public Type TargetContract => typeof(IService);
    
     
    
            public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    
            {
    
                return;
    
            }
    
     
    
            public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    
            {
    
                return;
    
            }
    
     
    
            public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    
            {
    
                dispatchRuntime.MessageInspectors.Add(new ServerMessageLogger());
    
            }
    
     
    
            public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    
            {
    
                return;
    
            }
    
    }

    Client-side.

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                Uri uri = new Uri("http://localhost:1300");
    
                IService service = ChannelFactory<IService>.CreateChannel(new BasicHttpBinding(), new EndpointAddress(uri));
    
                try
    
                {
    
                    Console.WriteLine(service.SayHello());
    
                }
    
                catch (Exception ex)
    
                {
    
                    Console.WriteLine(ex.ToString());
    
                }
    
            }
    
        }
    
     
    
        [ServiceContract(Namespace = "mydomain")]
    
        [CustomContractBehavior]
    
        public interface IService
    
        {
    
            [OperationContract]
    
            string SayHello();
    
        }
    
     
    
        public class ClientMessageLogger : IClientMessageInspector
    
        {
    
            public void AfterReceiveReply(ref Message reply, object correlationState)
    
            {
    
                string displayText = $"the client has received the reply:\n{reply}\n";
    
                Console.Write(displayText);
    
               
    
            }
    
     
    
            public object BeforeSendRequest(ref Message request, IClientChannel channel)
    
            {
    
                request.Headers.Add(MessageHeader.CreateHeader("myheader", "mynamespace", "myvalue"));
    
                string displayText = $"the client send request message:\n{request}\n";
    
                Console.WriteLine(displayText);
    
                return null;
    
            }
    
        }
    
        public class CustomContractBehaviorAttribute : Attribute, IContractBehavior, IContractBehaviorAttribute
    
        {
    
            public Type TargetContract => typeof(IService);
    
     
    
            public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    
            {
    
                return;
    
            }
    
     
    
            public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    
            {
    
                clientRuntime.ClientMessageInspectors.Add(new ClientMessageLogger());
    
            }
    
    
            public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    
            {
    
                return;
    
            }
    
            public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    
            {
    
                return;
    
            }
    
        }

    Result.

    You could use Message.Createheader() to create your custom header and request.Headers.RemoveAt() to remove the specific header, here is some links may be useful to you.

    https://stackoverflow.com/questions/4925759/adding-custom-soap-headers-from-silverlight-client

    https://weblogs.asp.net/paolopia/handling-custom-soap-headers-via-wcf-behaviors

     

    Feel free to let me know if you have any questions

    Best Regards

    Abraham


    Thursday, September 6, 2018 1:34 AM
    Moderator