0

I was able to connect to soap service and pass authentication token to it but every time I call service there 2 calls instead of 1.

First is without the authentication token and second is with the authentication token. Breakpoint in DocumentPaymentsReques was triggered only ones. Any suggestion why there are two calls?

Reference.cs

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
    if ((endpointConfiguration == EndpointConfiguration.KsiegowoscService))
    {
        System.ServiceModel.Channels.CustomBinding result = new System.ServiceModel.Channels.CustomBinding();
        System.ServiceModel.Channels.TextMessageEncodingBindingElement textBindingElement = new System.ServiceModel.Channels.TextMessageEncodingBindingElement();
        result.Elements.Add(textBindingElement);
        System.ServiceModel.Channels.HttpsTransportBindingElement httpsBindingElement = new System.ServiceModel.Channels.HttpsTransportBindingElement();

        httpsBindingElement.AllowCookies = true;
        httpsBindingElement.MaxBufferSize = int.MaxValue;
        httpsBindingElement.MaxReceivedMessageSize = int.MaxValue;
        httpsBindingElement.AuthenticationScheme = System.Net.AuthenticationSchemes.Basic;
        httpsBindingElement.ProxyAuthenticationScheme= System.Net.AuthenticationSchemes.Basic;
        httpsBindingElement.RequireClientCertificate = false;

        result.Elements.Add(httpsBindingElement);
    
        return result;
    }

    throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}

Calling method:

public void Execute(string auth, string companyId, string BKNumber, string DocumentNumber)
{
    SoapService.InvoiceClient client = new SoapService.InvoiceClient();

    client.ClientCredentials.UserName.UserName = "<login>";
    client.ClientCredentials.UserName.Password = "<password>";

    client.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication()
    {
        CertificateValidationMode = X509CertificateValidationMode.None,
        RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck
    };

    var request = new DocumentPaymentsRequest()
    {
        BKNumber = BKNumber,
        CompanyID = Convert.ToInt32(companyId),
        DocumentNumber = DocumentNumber,
        RequestGuid = Guid.NewGuid().ToString()
    };

    var result = client.DocumentPaymentsAsync(request).Result;
}

Result:

enter image description here

volfk
  • 99
  • 9
  • That is normal bevhaviour for Http authentication. You can bypass it by controlling your own outgoing request, such as [this](https://stackoverflow.com/questions/7089374/pre-authenticate-web-service-request-in-c-sharp/7902493) – ESG May 10 '21 at 20:03
  • I used this solution in my older project but in here System.ServiceModel.ClientBase doesn't have GetWebRequest method – volfk May 10 '21 at 21:41
  • It looks like you might need to set the header yourself, either directly or through an inspector: https://stackoverflow.com/questions/26366045/preemptive-authentication-with-custombinding That being said, is it the first 401 error being an issue? – ESG May 10 '21 at 22:12
  • 401 I have always becouse of error in serwis =) Issue is that there is 2 calls instead 1 – volfk May 11 '21 at 06:36

0 Answers0