Android Question Ntlm Again

Megasoft

New Member
Licensed User
Longtime User
Hello,

This is our scenario :
We have a Ms Dynamics NAV server with a lot of Navision Web Services published in enterprise intranet, all these web services needs ntlm authentication to work.
We have a IIS server that publish these web services with ntlm authentication, this part isolate intranet from extranet.
And finally we are working in a B4A app that works with these web services (published though IIS), and here is our problem, to use these Web Services we need to ntlm authenticate our app in our IIS Server.

I have a lot of doubts, i had read all about ntlm auth that i found in this forums (https://www.b4x.com/android/forum/threads/integrated-windows-authentication-ntlmv2-solution.66790/, https://www.b4x.com/android/forum/t...s-using-httputils2-library.52581/#post-496848 , https://www.b4x.com/android/forum/threads/where-can-i-find-the-package-com-squareup-okhttp.93772/ , https://www.b4x.com/android/forum/threads/ntlm-authentication.94350/#post-596972 , etc .. ).
I had downloaded ntlm_test1.zip example of Mr23, I unzip and tried to compile but obtain the same error of Marco Gioia:

B4J Version: 6.30
Parsing code. (0.06s)
Compiling code. (0.20s)
Compiling layouts code. (0.01s)
Organizing libraries. (0.00s)
Compiling generated Java code. Error
javac 1.8.0_144
src\b4j\example\main.java:16: error: package com.squareup.okhttp does not exist
import com.squareup.okhttp.Authenticator;
^
1 error

I had read again the post https://www.b4x.com/android/forum/threads/integrated-windows-authentication-ntlmv2-solution.66790/ but i not sure where is the problem, com.squareup.okhttp libray? OkHttpUtils2 library? I am new in B4A (I have knowledge in VB, .NET, Navision, IIS, MSSql Server but B4A and B4J are new for me).
Please could you help m

Thanks for your attention
 

Megasoft

New Member
Licensed User
Longtime User
Thank you very much Erel, this will help us a lot because all our applications are made with B4A and we want to continue using it.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Follow these steps, note that it wasn't fully tested:

0.Update to OkHttpUtils2 v2.80+: https://www.b4x.com/android/forum/threads/updates-to-internal-libraries.59340/#post-690638
1. Add to the main module:
B4X:
#AdditionalJar: httpclient-4.5.2
#AdditionalJar: httpcore-4.4.4
#AdditionalJar: commons-codec-1.9
You will need to add these jars to the additional libraries folder. You can find them in B4J internal libraries folder.

2. Add build configuration symbol: HU2_PUBLIC

3. Add to starter service:
B4X:
Sub Service_Create
   Dim jo As JavaObject = HttpUtils2Service.hc
   Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
   Dim ntlm As JavaObject
   ntlm.InitializeNewInstance(Application.PackageName & ".starter$NTLMAuthenticator", Array("username", "password", "domain", "workstation")) 'replace with your own values
   builder.RunMethod("authenticator", Array(ntlm))
   jo.SetField("client", builder.RunMethod("build", Null))
End Sub

#if JAVA
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.Proxy;
import java.util.List;

import org.apache.http.impl.auth.NTLMEngine;
import org.apache.http.impl.auth.NTLMEngineException;
import org.apache.http.impl.auth.NTLMScheme;


import okhttp3.Authenticator;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

   public static class NTLMAuthenticator implements Authenticator {
    final NTLMEngine engine;
    private final String domain;
    private final String username;
    private final String password;
    private final String ntlmMsg1;
    private final String workstation;

    public NTLMAuthenticator(String username, String password, String domain, String workstation) throws Exception {
    this.domain = domain;
    this.username = username;
    this.workstation = workstation;
    this.password = password;
    NTLMScheme scheme = new NTLMScheme();
    Field f = scheme.getClass().getDeclaredField("engine");
    f.setAccessible(true);
    engine = (NTLMEngine)f.get(scheme);
    String localNtlmMsg1 = null;
         localNtlmMsg1 = engine.generateType1Msg(domain, workstation);
    ntlmMsg1 = localNtlmMsg1;
    }

    @Override
    public Request authenticate(Route route, Response response) throws IOException {
    final List<String> WWWAuthenticate = response.headers().values("WWW-Authenticate");
    if (WWWAuthenticate.contains("NTLM")) {
    return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg1).build();
    }
    String ntlmMsg3 = null;
  try {
         ntlmMsg3 = engine.generateType3Msg(username, password, domain, workstation, WWWAuthenticate.get(0).substring(5));
       } catch (NTLMEngineException e) {
         throw new RuntimeException(e);
       }
    return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg3).build();
    }
   
   }
   
#end if
 
Upvote 0

Chris Guanzon

Active Member
Licensed User
Longtime User
#AdditionalJar: httpclient-4.5.2

You will need to add these jars to the additional libraries folder. You can find them in B4J internal libraries folder.

2. Add build configuration symbol: HU2_PUBLIC

[/code]

i can't find this (httpclient-4.5.2.jar) in B4J internal libraries. Where can i find this? I am using b4j 8.10.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top