Header Object
[Serializable]
[DataContract(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")] // This object serialize specific namespace
public class Security
{
[DataMember] // This object serialize without namespace
public UsernameToken UsernameToken;
}
public class UsernameToken : IXmlSerializable
{
public string Username { get; set; }
public string Password { get; set; }
public XmlSchema GetSchema() { return null; }
public void ReadXml(System.Xml.XmlReader reader)
{
reader.MoveToContent();
Username = reader.ReadElementString("Username");
reader.ReadStartElement();
Password = reader.ReadElementString("Password");
reader.ReadEndElement();
}
public void WriteXml(XmlWriter writer)
{
writer.WriteElementString("Username", Username);
writer.WriteElementString("Password", Password);
}
}
Set endpoint header
SubscriptionWSImplServiceClient client = new SubscriptionWSImplServiceClient();
//Create wsse security object
Security security = new Security();
UsernameToken usernameToken = new UsernameToken { Password = password, Username = uname };
security.UsernameToken = usernameToken;
//Serialize object to xml
XmlObjectSerializer xmlObjectSerializer = new DataContractSerializer(typeof(Security), "Security", "");
//Create address header with security header
AddressHeader addressHeader = AddressHeader.CreateAddressHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", security, xmlObjectSerializer);
//Create new endpoint with addressHeader that contains security header
EndpointAddress endpoint = new EndpointAddress(new Uri("http://127.0.0.1/mpp/subscriptionService"), new[] { addressHeader });
//Set new endpoint
client.Endpoint.Address = endpoint;