如何将安全标头发送到WCF服务

问题描述 投票:0回答:1

您好我一直在与WCF服务集成。我使用WSDL链接生成对WCF的调用。

我总是收到一条消息SOAP Header中需要用户名/密码,但我不知道我做错了什么。

当我通过SOAPUI完成请求工作时...

问题是该方法接收两个对象。第一个是普通有效载荷,第二个参数是这种类型:

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;
        }
    }
}

我正在通过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")
            ))))));

如果有人可以帮助我,我会很感激!

wcf soap asp.net-core wsdl xelement
1个回答
0
投票
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;

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.