Search code examples
wcf.net-core

How can I attach BinarySecurityToken in request header(.net core)


I want to create something like below

<soapenv:Header>
<wsse:Security>
<wsse:BinarySecurityToken EncodingType="XXXX" ValueType="XXX">Token
</wsse:BinarySecurityToken>
</wsse:Security>
</soapenv:Header>

I manage to acheive this by using the below code, which is working (well Kind of!)

using (new OperationContextScope(experianProxy.InnerChannel))
{
DataContractJsonSerializer serializer =new 
DataContractJsonSerializer(typeof(BinarySecurityToken));

MessageHeader header = MessageHeader.CreateHeader("wsse:Security", "", 
_token,serializer);

OperationContext.Current.OutgoingMessageHeaders.Add(header);
var interactiveResponse = experianProxy.InteractiveAsync(new Root()).Result;
return interactiveResponse.OutputRoot.ToString();
}

and

[XmlRoot(ElementName = "wsse:BinarySecurityToken", Namespace = "")]
public sealed class BinarySecurityToken : IXmlSerializable
{...}

Now the request going out is,

<soapenv:Header>
<wsse:Security>&lt;wsse_x003A_BinarySecurityToken ValueType="xxxx" 
EncodingType="wsse:Base64Binary"
&gt;XXXXXXXXXXX&lt;/wsse_x003A_BinarySecurityToken&gt;
</wsse:Security>
</soapenv:Header>

Converting cdata didn't help.

Can someone please point out what am I missing? Any help is much appreciated.

Thank you.


Solution

  • The scope of OperationContextScope is only valid within the using statement. after the instance of OperationContextScope is released, the OperationContext is restored and the message header is no longer valid. if you try to call the method in the using statement, you will find your custome header.

    You can use the IClientMessageInspector interface if you want to permanently add message headers to requests.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/f1f29779-0121-4499-a2bc-63ffe8025b21/wcf-security-soap-header