WCF生成OASIS身份验证对象

时间:2019-01-30 03:37:08

标签: c# asp.net .net wcf soap

我正在尝试使用Visual Studio中的“添加serviceReference”功能来调用SOAP。 SOAP身份验证方法应使用OASIS完成。标头应类似于

  <Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
  <wsse:UsernameToken  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <wsse:Username></wsse:Username>
    <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">monMonDePasse</wsse:Password>
    <wsse:Nonce>sdsdsdlojhfdsdM5Nw==</wsse:Nonce>
    <wsu:Created>2019-01-21T6:17:34Z</wsu:Created>
  </wsse:UsernameToken>
</Security>

所有类均已成功生成,而没有必须手动添加的UsernameToken和Security类。

var UsernameToken = new UsernameToken{ Username = userName, Password = password, Nonce = nonce, Created = created };

我正在使用以下代码在标题中添加安全性:

Security security = new Security { UsernameToken =UsernameToken  }; 
System.ServiceModel.Channels.MessageHeader messageHeader = 
System.ServiceModel.Channels.MessageHeader.CreateHeader(name: "Security", 
ns: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity- 
secext-1.0.xsd", value: security, mustUnderstand: true);

我的问题是手动创建的对象的输出名称空间不正确。这是生成的请求

  <UsernameToken
xmlns="http://schemas.datacontract.org/2004/07/ProjectName.UnitTesting"> //This is not the correct namespace
<Created>2019-01-21T06:42:15Z</Created>
<Nonce>NzUyZg==</Nonce>
<Password>MonUserName=</Password>
<Username>MonPassword</Username>

我想将usernameToken命名空间设置为

 xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"

代替

"http://schemas.datacontract.org/2004/07/ProjectName.UnitTesting"

我尝试添加属性

[XmlAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]

并且仍然面临相同的问题。我还面临与serviceContract属性相同的问题。 谢谢,

1 个答案:

答案 0 :(得分:0)

一种方法是使用XmlElemnt添加前缀,另一种方法是在web.config或app.config中添加标头。

下面是如何编写代码,请添加您自己的标头。

     using (ChannelFactory<ICalculatorService> ChannelFactory = new ChannelFactory<ICalculatorService>("cal"))
        {
                            ICalculatorService employeeService = ChannelFactory.CreateChannel();
            using (OperationContextScope scope = new OperationContextScope((IContextChannel)employeeService))
            {

                System.Xml.XmlDocument document = new XmlDocument();


                XmlElement element = document.CreateElement("wsse", "UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");


                XmlElement newChild = null;

                newChild = document.CreateElement("wsse", "Username", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
                newChild.InnerText = "finance";
                element.AppendChild(newChild);

                newChild = document.CreateElement("wsse", "password", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
                newChild.SetAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
                newChild.InnerText = "387";
                element.AppendChild(newChild);

                MessageHeader messageHeader = MessageHeader.CreateHeader("security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", element, false);


                OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);
                employeeService.Add(5, 6);
            }

                           Console.Read();
        }

您还可以添加app.config或web.config。

<headers>
<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsse:Username></wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">monMonDePasse</wsse:Password><wsse:Nonce>sdsdsdlojhfdsdM5Nw==</wsse:Nonce><wsu:Created>2019-01-21T6:17:34Z</wsu:Created></wsse:UsernameToken></Security>
  </headers>

结果。

enter image description here

关于在app.config中编写,如果您不想  要显示,请将所有标题放在同一行中,就像我在我的app.config中发布一样。