Ted S.
Ted S.

Reputation: 77

WCF: Matching a specific WS-Security scheme (Signature, Encrypt, UserPass)

I'm trying to match a specific WS-Security specification from a vendor. The following works in SOAP UI, which I am now trying to recreate in C# WCF (targeting .NET 4.7.2 or 4.8):

Everything I've tried so far seems to encrypt everything in the WS-Security header except for the timestamp.

References followed:


SOAP UI Setup - Signature:

SOAPUI-Signature

SOAP UI Setup - Encryption:

SOAPUI-Encryption

Upvotes: 0

Views: 492

Answers (1)

Ding Peng
Ding Peng

Reputation: 3974

You can try to use IClientMessageInspectortor add a header to the message. In the BeforeSendRequest method, you can add a custom header to the outgoing message.Here is a demo:

      public class CustomMessageInspector : IClientMessageInspector
{
    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        return;
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        MessageHeader header = MessageHeader.CreateHeader("Testreply", "http://Test", "Test");
        request.Headers.Add(header);
        return null;
    }
}
[AttributeUsage(AttributeTargets.Interface)]
public class CustContractBehaviorAttribute : Attribute, IContractBehavior
{
    public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
        return;
    }

    public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.ClientMessageInspectors.Add(new CustomMessageInspector());
    }
    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
        return;
    }

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {
        return;
    }
}

Add CustContractBehaviorAttribute to apply it:

enter image description here

Upvotes: 0

Related Questions