Search code examples
perlweb-servicessoapnavisionsoaplite

Perl SOAP::Lite service - Authentification with NTLM and Navision 2009 SP1


I'm trying to connect to a webservice exposed by Navision 2009 SP1, which needs NTLM authentification.

The authentification works for the service() call - meaning I get the stubs returned! However, as soon as I try to call a function, the authentification is gone.

I've tried setting the credentials again for the $service (which then lacks keep_alive), and I've also tried using the PATCH_HTTP_KEEPALIVE constant, but both didn't help.

$SOAP::Constants::PATCH_HTTP_KEEPALIVE = 1; # does this even do anything?

my $ua = LWP::UserAgent->new(keep_alive => 1);
$ua->credentials($host, '', $domain_user, $password);

my $client = SOAP::Lite->new();
$client->schema->useragent($ua);

# this works!
my $service = $client->service($service_url)->proxy($service_url);

# this is just a test - how can I set keep_alive again?
$service->{_transport}->{_proxy}->credentials($host, '', $domain_user, $password);

# this returns 401 unauthorized
$service->Read('R1446781');

This is the full response I get:

SOAP::Transport::HTTP::Client::send_receive: HTTP::Request=HASH(0x224adf8)
SOAP::Transport::HTTP::Client::send_receive: POST http://somedomain.org:7047/DynamicsNAV/WS/Company%20Name/Page/InvHeader HTTP/1.1
Accept: text/xml
Accept: multipart/*
Accept: application/soap
Content-Length: 485
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:microsoft-dynamics-schemas/page/invheader:Read"

<?xml version="1.0" encoding="UTF-8"?><soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:microsoft-dynamics-schemas/page/invheader" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><tns:Read><No xsi:type="xsd:string">R1446781</No></tns:Read></soap:Body></soap:Envelope>
The keep_alive option must be enabled for NTLM authentication to work.  NTLM authentication aborted.
SOAP::Transport::HTTP::Client::send_receive: HTTP::Response=HASH(0x22511b8)
SOAP::Transport::HTTP::Client::send_receive: HTTP/1.1 401 Unauthorized
Date: Fri, 06 Feb 2015 09:42:22 GMT
Server: Microsoft-HTTPAPI/2.0
WWW-Authenticate: NTLM
Content-Length: 0
Client-Date: Fri, 06 Feb 2015 09:42:22 GMT
Client-Peer: somedomain.org:7047
Client-Response-Num: 1

401 Unauthorized at script/test_webservice.pl line 36.

How can I authenticate the $service functions using the same authentification I've already used for the service() call?


Solution

  • Figured it out:

    $service->transport->proxy->conn_cache($ua->conn_cache);

    It has to use the same connection cache, then it works.