Home > Enterprise >  Authentication Soap Api Header c#
Authentication Soap Api Header c#

Time:06-25

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)'

CodePudding user response:

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);


        }
    }
  
}

CodePudding user response:

If I use the builder in this way, it work.

var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
  • Related