We were able to solve it with the following code:
public class SecurityHeader : System.ServiceModel.Channels.MessageHeader {
public string userName;
public string password;
protected override void OnWriteStartHeader (System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion messageVersion)
{
writer.WriteStartElement("wsse", Name, Namespace);
writer.WriteXmlnsAttribute("wsse", Namespace);
}
protected override void OnWriteHeaderContents (System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion messageVersion)
{
writer.WriteStartElement("wsse", "UsernameToken", Namespace);
writer.WriteStartElement("wsse", "Username", Namespace);
writer.WriteValue(userName);
writer.WriteEndElement();
writer.WriteStartElement("wsse", "Password", Namespace);
writer.WriteValue(password);
writer.WriteEndElement();
writer.WriteEndElement();
}
public override string Name
{
get { return "Security"; }
}
public override string Namespace
{
get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; }
}
}
This wrote the header that was required by the DataPower box.
How to use the class SecurityHeader
public static void Main(string[] args)
{
var webService = new ServiceReference1.MyWebService();
....
webService.Open();
using (OperationContextScope scope = new OperationContextScope((IContextChannel)webService.InnerChannel))
{
var myObjRequest = GetMyObjRequest();
MessageHeaders messageHeadersElement = OperationContext.Current.OutgoingMessageHeaders;
messageHeadersElement.Add(SecurityHeader("UserName", "Password"))
var res = webService.MyServe(myObjRequest);
Console.WriteLine(res.ToString());
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…