Search code examples
androidauthenticationksoap2ntlm

NTLM Authentication with ksoap2 in Android


I am using ksoap2 3.6.

I am trying to access a soap web service secured with NTLM. I found the ksoap2-extra-ntlm-3.6.0.jar file and added that to my project, thinking this was my saving grace.

NtlmTransport ntlmTransport = new NtlmTransport(BASE_URL);
ntlmTransport.setCredentials(USERNAME, PASSWORD, DOMAIN, "");
ntlmTransport.call(name_space + method_name, envelope);

However, I get an unauthorized 401 response code. I have validated my username, password and domain via .NET and browser based authentication methods, so I know those are correct.

Question: how do I actually authenticate and get past the 401 code?

Thanks.


Solution

  • This is what worked for me.

    Since ksoap2 is open-source, I peeked at the code for NtlmTransport and altered the call method in the following way.

    I removed:

    BasicHttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, this.timeout);
    
    DefaultHttpClient client = new DefaultHttpClient(httpParameters);
        client.getAuthSchemes().register("ntlm", new NtlmTransport.NTLMSchemeFactory());
    
    NTCredentials credentials = new NTCredentials(this.user, this.password, this.ntWorkstation, this.ntDomain);
    
    client.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
    

    and replaced that code with this code:

    NTCredentials ntCredentials = new NTCredentials(user, password, ntWorkstation, ntDomain );
    
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY), ntCredentials);
    
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    
    clientBuilder.useSystemProperties();
    clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    CloseableHttpClient client = clientBuilder.build();
    

    I wrapped the execute and response code with a try...finally block to close the client in the finally block. Works like a charm now.

    I added

    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
    

    to my gradle file.

    Thought somebody might benefit.

    Thanks.