我正在尝试使用visual studio中的"add 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和安全类。
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);我的问题是手动创建的对象的输出nameSpace不正确。下面是生成的请求
<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属性,我也面临着同样的问题。谢谢,
发布于 2019-01-30 14:59:43
一种方式是使用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>结果就是。

关于用app.config写东西,如果你不想出现,请把你所有的标题放在同一行,就像我在我的app.config中发表的那样。
https://stackoverflow.com/questions/54433010
复制相似问题