简体   繁体   中英

Authentication Soap Api Header c#

i'm trying to call a soap api but i can't authenticate. The api should recive an xml like this in the header:

<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. (Error: UsernameToken not present)'

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM