How to send security header to WCF Service

Hi I have been working on an integration with a WCF service.. I have use a WSDL link to generate the call to the WCF.

I'm always receive a message Username/Password are required in SOAP Header but I don't know what I'm doing wrong.

When I made the request via SOAPUI work perfectly..

The problem is that the method receive two objects. The first is the normal payload and the second param is of this type:

public partial class SecurityHeaderType
{

    private System.Xml.Linq.XElement[] anyField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAnyElementAttribute(Order=0)]
    public System.Xml.Linq.XElement[] Any
    {
        get
        {
            return this.anyField;
        }
        set
        {
            this.anyField = value;
        }
    }
}

I'm creating an envelope via XElements:

XElement securityHeaderXml = new XElement(soapenv + "Envelope",
    new XAttribute(XNamespace.Xmlns + "soapenv", "http://schemas.xmlsoap.org/soap/envelope/"),
    new XAttribute(XNamespace.Xmlns + "oas", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"),
    new XAttribute(XNamespace.Xmlns + "ns", "http://www.opentravel.org/OTA/2003/05"),
    new XElement(soapenv + "Header",
    new XElement(oas + "Security",
      new XElement(oas + "UsernameToken",
        new XElement(oas + "Username", "someusername"),
        new XElement(oas + "Password", "somepassword",
          new XAttribute("Type",
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"))
      ))), new XElement(soapenv + "Body",
      new XElement(ns + "OTA_HotelAvailNotifRQ",
        new XAttribute("EchoToken", "abc123"),
        new XAttribute("Version", "1.0"),
        new XAttribute("ResStatus", "Commit"),
        new XAttribute("TimeStamp", DateTime.Now),
        new XElement("AvailStatusMessages", new XAttribute("HotelCode", hotelCode)
          , new XElement("AvailStatusMessage", new XAttribute("BookingLimit", 10),
            new XElement("StatusApplicationControl", new XAttribute("Start", DateTime.Now),
              new XAttribute("End", DateTime.Now.AddDays(4)),
              new XAttribute("InvTypeCode", "A1K"), new
                XAttribute("RatePlanCode", "GLD")
            ))))));

If someone can help me i would appreciate it!!

Upvotes: 0

Views: 460

Answers (1)

Yingyi Wang
Yingyi Wang

Reputation: 11

public class SecurityHeader
{
   private SecurityHeaderType security;
   public SecurityHeaderType GetSecurityHeaderType(string username,string 
   password,string edmid)
   {
      security = new SecurityHeaderType();
      DateTime created = DateTime.Now;
      XmlDocument doc = new XmlDocument();
      using (XmlWriter writer = doc.CreateNavigator().AppendChild())
      {
         writer.WriteStartDocument();
         writer.WriteStartElement("oas",     
"Security",CMISNameSpaces.oasNamespace);
            writer.WriteStartElement("oas", "UsernameToken", 
CMISNameSpaces.oasNamespace);
            writer.WriteElementString("oas", "Username", 
CMISNameSpaces.oasNamespace, username);
            writer.WriteElementString("oas", "Password", 
CMISNameSpaces.oasNamespace, password);
            writer.WriteElementString("edm", "edmAppID", 
CMISNameSpaces.edmNamespace, edmid);

            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
        }

        doc.DocumentElement.RemoveAllAttributes();
        System.Xml.XmlElement[] headers = 
doc.DocumentElement.ChildNodes.Cast<XmlElement>().ToArray<XmlElement>();

        security.Any = headers;

        return security;

    }

}

Upvotes: 1

Related Questions