Consume WS with secutity 'Authentificate pre-emptively'

211 Views Asked by At

I'm trying to connect To WS with WS security.

I did test it with SOAP UI:

enter image description here

enter image description here

it works fine.

I did try many ways with C# code but always exceptions: for example this method:

using (var client = new DocumentWSWs.DocumentUploadWSClient())
{

    DocumentWSWs.StatusRequest currentStatusRequest = new DocumentWSWs.StatusRequest();

    client.ClientCredentials.UserName.UserName = UserNameWs;
    client.ClientCredentials.UserName.Password = PasswordWs;
    SetTlsSecurite();

    DocumentWSWs.StatusResponse getCurrentStatusResponse = client.GetStatus(currentStatusRequest);
}

with app.config :

<endpoint address="https://*************/DocumentUploadWS.wsdl"
            binding="basicHttpBinding" bindingConfiguration="DocumentUploadWSSoap11"
            contract="DocumentWSWs.DocumentUploadWS" name="DocumentEndPoint" >
            <headers>
                <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                    <wsse:UsernameToken  Id="UsernameToken-49">
                        <wsse:Username>*******</wsse:Username>
                        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">***************</wsse:Password>
                    </wsse:UsernameToken>
                </wsse:Security>
            </headers>
    </endpoint>

    <binding name="DocumentUploadWSSoap11">
                <security mode="Transport">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
    </binding>

But I’m getting: The HTTP request is not authorized with the 'Anonymous' client authentication scheme

I did try security mode="TransportWithMessageCredential"

But I got this error:

Could not find default endpoint element referencing contract ' DocumentUploadWSSoap11.DocumentUploadWS' in Client Configuration Service ServiceModel. It is possible that no configuration file was found for the application or that an endpoint element corresponding to the contract was not found in the client element.

Despite checking many blogs and forums, I found errors. Or the blogs were unclear on how to use the code given.

Can someone give me the right direction or the way to call this WS

Thank you.

1

There are 1 best solutions below

3
hehe On

The credentials are taken correctly

Configure the endpoint and binding programmatically (if you don't want to use the configuration from the app.config). You can create a custom binding and endpoint like this:

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

EndpointAddress endpointAddress = new EndpointAddress("https://*************");

using (var client = new DocumentWSWs.DocumentUploadWSClient(binding, endpointAddress))
{
    client.ClientCredentials.UserName.UserName = UserNameWs;
    client.ClientCredentials.UserName.Password = PasswordWs;

    // Now, you can call the service methods.
}

Now make the service calls:

DocumentWSWs.StatusRequest currentStatusRequest = new DocumentWSWs.StatusRequest();
DocumentWSWs.StatusResponse getCurrentStatusResponse = client.GetStatus(currentStatusRequest);

---EDIT---

If you are only provided with the information required to connect to the web service using SOAP UI with pre-authentication and the client hasn't provided specific configuration details, you can try to mimic the SOAP UI configuration in your C# client as closely as possible.

  1. In SOAP UI, open the project and request that successfully connects to the web service
  2. Capture the configuration settings used in SOAP UI
  3. In SOAP UI, check how the username and password are configured for authentication. Note the authentication method (e.g., Basic Authentication).

After checking any other configurations configure the client. here is an example:

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

EndpointAddress endpointAddress = new EndpointAddress("https://your-service-endpoint-url");

using (var client = new DocumentWSWs.DocumentUploadWSClient(binding, endpointAddress))
{
    client.ClientCredentials.UserName.UserName = "your-username";
    client.ClientCredentials.UserName.Password = "your-password";

    // Make the service call here
}