[英]Authentication Soap Api Header c#
i'm trying to call a soap api but i can't authenticate.我正在尝试调用soap api,但无法进行身份验证。 The api should recive an xml like this in the header: api 应该在标题中接收这样的 xml:
<wsse:Security>
<wsse:UsernameToken>
<wsse:Username>XXXXXXXXX</Username>
<wsse:Password>XXXXXXXXX</Password>
</wsse:UsernameToken>
</wsse:Security>
But i don't understand how to pass it, i'm trying to insert the credential in this way:但我不明白如何通过它,我试图以这种方式插入凭证:
client.ClientCredentials.UserName.UserName = "XXXXXXXXX";
client.ClientCredentials.UserName.Password = "XXXXXXXXX";
And then build the binder in this way:然后以这种方式构建活页夹:
BasicHttpsBinding bindingHttps = new BasicHttpsBinding();
bindingHttps.Security.Mode = BasicHttpsSecurityMode.Transport;
bindingHttps.MaxReceivedMessageSize = 500000000;
bindingHttps.SendTimeout = new TimeSpan(0, 5, 0);
But the call return this error: System.AggregateException: 'One or more errors occurred.但调用返回此错误: System.AggregateException: '发生一个或多个错误。 (Error: UsernameToken not present)' (错误:UsernameToken 不存在)'
Try following :尝试以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
//This is Xml you need to generate
//<? xml version = "1.0" ?>
//<soapenv:Envelope xmlns:wsse="http://schemas.xmlsoap.org/soap/envelope/" >
// <wsse:Header >
// <wsse:Security >
// <wsse:UsernameToken >
// <wsse:Username > XXXXXXXXX </ Username >
// <wsse:Password > XXXXXXXXX </ Password >
// </wsse:UsernameToken >
// </wsse:Security >
// </wsse:Header >
// <wsse:Body >
// <yourbodygoeshere >
// </wsse:Body >
//</wsse:Envelope >
namespace ConsoleApplication23
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string username = "John";
string password = "1234567890";
string ident = "<?xml version=\"1.0\"?><wsse:Envelope xmlns:wsse=\"http://schemas.xmlsoap.org/soap/envelope/\"></wsse:Envelope>";
XDocument doc = XDocument.Parse(ident);
XElement envelope = doc.Root;
XNamespace wsseNs = envelope.GetNamespaceOfPrefix("wsse");
XElement header = new XElement(wsseNs + "Header");
envelope.Add(header);
XElement security = new XElement(wsseNs + "Security",
new XElement(wsseNs + "UsernameToken",
new XElement(wsseNs + "Username", username),
new XElement(wsseNs + "Password", password)
)
);
header.Add(security);
XElement body = new XElement(wsseNs + "Body");
envelope.Add(body);
}
}
}
If I use the builder in this way, it work.如果我以这种方式使用构建器,它可以工作。
var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.