WCF 生成 OASIS 身份验证 object
WCF generate OASIS authentication object
我正在尝试使用 visual studio 中的 "add serviceReference" 功能调用 SOAP。 SOAP 身份验证方法应该使用 OASIS 来完成。 header 应该类似于
<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 };
我正在使用以下代码在 header 中添加安全性:
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);
我的问题是手动创建的 object 的输出名称空间不正确。这是生成的请求
<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 属性相同的问题。
谢谢,
一种方法是使用XmlElemnt添加前缀,另一种方法是在web.config或app.config中添加header。
下面是代码的写法,请自行添加header。
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,如果你不想
出现,请把你们 header 放在同一行,只有我 post 在我的 app.config.
我正在尝试使用 visual studio 中的 "add serviceReference" 功能调用 SOAP。 SOAP 身份验证方法应该使用 OASIS 来完成。 header 应该类似于
<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 };
我正在使用以下代码在 header 中添加安全性:
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);
我的问题是手动创建的 object 的输出名称空间不正确。这是生成的请求
<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 属性相同的问题。 谢谢,
一种方法是使用XmlElemnt添加前缀,另一种方法是在web.config或app.config中添加header。
下面是代码的写法,请自行添加header。
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,如果你不想 出现,请把你们 header 放在同一行,只有我 post 在我的 app.config.