1

I need to connect Bodet KELIO SOAP Service from a C# application. The documentation for connecting the service is here : https://kelio.help.kelio.io/V5.1P5/fr-FR/webservices/index.html

Test queries are OK from SOAP-UI

I tried to generate the code from the connected service in my projet, using the visual studio 2022 wizard from the WSDL located : https://sandbox-ws.kelio.io/open/services/LightEmployeeService?wsdl user is : api-ws password : ws-sandbox

But when I try to consume it with VS

void SalarieTEst()
{
    Salaries.LightEmployeeServicePortTypeClient client = new Salaries.LightEmployeeServicePortTypeClient();
    client.ClientCredentials.UserName.UserName = "api-ws";
    client.ClientCredentials.UserName.Password = "api-sandbox";

    var toto = client.exportLightEmployees("", "");
}

I get the following error :

System.ServiceModel.Security.MessageSecurityException : 'La requête HTTP n'est pas autorisée avec un schéma d'authentification client 'Anonymous'. L'en-tête d'authentification reçu du serveur était 'Basic realm="Bodet Realm"'.'

I tried the code generated by SOAPUI instead, I get a security connection error, but, the result seems to be mismatched, every data are null.

At last, I try to simply query with a standard HttpClient:

async Task TestSoap()
{
    var url = @"https://sandbox-ws.kelio.io/open/services/LightEmployeeService";
    var soapEnveloppe = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ech=""http://echange.service.open.bodet.com"">
<soapenv:Header/>
<soapenv:Body>
<ech:exportLightEmployeesList>
<!--Optional:-->
<ech:exportFilter>
<!--Zero or more repetitions:-->
<ech:AskedPopulation>
<!--Optional:-->
<ech:populationFilter>Kelio Démonstration</ech:populationFilter>
<!--Optional:-->
<ech:groupFilter>Cadres</ech:groupFilter>
<!--Optional:-->
<ech:populationMode>0</ech:populationMode>
</ech:AskedPopulation>
</ech:exportFilter>
</ech:exportLightEmployeesList>
</soapenv:Body>
</soapenv:Envelope>";

    string username = "api-ws";
    string password = "api-sandbox";
    string domain = "Bodet"; // Optional

    var AuthToken = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));

    using (var Client = new HttpClient())
    {
        var request = new HttpRequestMessage(HttpMethod.Post, url);
        Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", AuthToken);
        request.Content = new StringContent(soapEnveloppe, Encoding.UTF8, "text/xml");

        var response = await Client.PostAsync(url, request.Content);
    }
}

But the same http 403 error occurs.

I also tried with HttpClienderhandler and PreAuthenticate, but always get the same result....

Please help :)

2 Answers 2

0

I had similar issues as you.
When using a connected service, you need to add the authentication type to the binding when initializing the LightEmployeeServicePortTypeClient object, and also increase the MaxReceivedMessageSize.

After that, I was indeed receiving 204 employees, but with no values at all.

public void test1()
{
    EndpointAddress endpointAdress = new EndpointAddress("https://sandbox-ws.kelio.io/open/services/LightEmployeeService");
    BasicHttpsBinding binding = new BasicHttpsBinding();

    binding.MaxReceivedMessageSize = 1048576;

    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

    ServiceReference1.LightEmployeeServicePortTypeClient client = new ServiceReference1.LightEmployeeServicePortTypeClient(binding, endpointAdress);


    client.ClientCredentials.UserName.UserName = "api-ws";
    client.ClientCredentials.UserName.Password = "api-sandbox";

    ServiceReference1.LightEmployee[] response =  client.exportLightEmployees("Kelio Démonstration", "0");
}

So I switched to a simple POST request, like in your second attempt.

I ran into the same problem as well — error 403 — it was simply because the SOAPAction header was missing.

public async Task<string> SoapCallWAuth(string url, string SoapAction, string login, string pass, string PostBody)
{
    var AuthToken = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{login}:{pass}"));
    string returnXML = "";
    try
    {
        using (var Client = new HttpClient())
        {
            var request = new HttpRequestMessage(HttpMethod.Post, url);
            Client.DefaultRequestHeaders.Add("SOAPAction", SoapAction);
            Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", AuthToken);

            request.Content = new StringContent(PostBody, Encoding.UTF8, "text/xml");

            var response = await Client.PostAsync(url, request.Content);
            returnXML = await response.Content.ReadAsStringAsync();

        }
    }
    catch (Exception ex)
    {
    }
    return returnXML;
}
public async Task testFct()
{
    var url = @"https://sandbox-ws.kelio.io/open/services/LightEmployeeService";
    var soapEnveloppe = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ech=""http://echange.service.open.bodet.com"">
       <soapenv:Header/>
       <soapenv:Body>
          <ech:exportLightEmployees>
             <ech:populationFilter>Kelio Démonstration</ech:populationFilter>
             <ech:groupFilter>0</ech:groupFilter>
          </ech:exportLightEmployees>
       </soapenv:Body>
    </soapenv:Envelope>";

    try
    {

        string retourXML = await SoapCallWAuth(url, "urn:exportLightEmployees", "api-ws", "api-sandbox", soapEnveloppe);


        XmlDocument xmlDocument = new XmlDocument();
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
        nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
        nsmgr.AddNamespace("ns1", "http://echange.service.open.bodet.com");
        xmlDocument.LoadXml(retourXML);


        XmlNodeList NodeLists = xmlDocument.SelectNodes(@"/soap:Envelope/soap:Body/ns1:exportLightEmployeesResponse/ns1:exportedLightEmployees/ns1:LightEmployee", nsmgr);
        foreach (XmlNode Node in NodeLists)
        {
            Console.WriteLine(Node.InnerText);
        }
    }
    catch (Exception ex)
    {

    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

After that, I was indeed receiving 204 employees, but with no values at all.

I had similar issues too, running the code generated by Visual Studio 2022. I could solve it removing the Order property in the XmlElement Attributes :

[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=0)]

changed to

[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]

Order values generated by VS seem to be incorrect for inherited classes and cause empty properties in deserialized data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.