Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

WebAPI : NTLM authentication in Ax2012 R3

(1) ShareShare
ReportReport
Posted on by 45

Hi,

I'm actually trying to call a WebAPI from code.

The API is running on a IIS which has Windows Authentication activated

I've tried to call it in a job with the code below :

static void TestApi(Args _args)
{
    str                             url;
    str                             byteStr;
    System.Net.HttpWebRequest       request;
    System.Net.HttpWebResponse      response;

    System.Byte[]                   byteArray;
    System.IO.Stream                stream;
    System.IO.Stream                dataStream;
    System.IO.StreamReader          streamReader;
    System.Net.ServicePoint         servicePoint;
    System.Net.WebHeaderCollection  httpHeader;
    CLRObject                       clrObj, ex;
    System.Text.Encoding            utf8;
    xml responseXml;
    ;

    byteStr = strfmt('%1:%2', "toto", "tata");
    new InteropPermission(InteropKind::ClrInterop).assert();
    httpHeader = new System.Net.WebHeaderCollection();
    url = "http://atl-svax016.atelier.lan:8080/api/DMDLOG/Information/CHG-0000004536";
    clrObj      = System.Net.WebRequest::Create(url);
    request     = clrObj;
    request.set_Method("GET");
    request.set_KeepAlive(true);
    request.set_ContentType("application/json");
    utf8        = System.Text.Encoding::get_UTF8();
    byteArray   = utf8.GetBytes(byteStr);
    byteStr     = System.Convert::ToBase64String(byteArray);
    httpHeader.Add("Authorization", 'NTLM '   byteStr);
    

    try
    {
        request.set_Headers(httpHeader);
        response = request.GetResponse();
        dataStream = response.GetResponseStream();
        streamReader = new System.IO.StreamReader(dataStream);
        responseXml  = streamReader.ReadToEnd();
        info(responseXml);
    }
    catch (Exception::CLRError)
    {
        ex = ClrInterop::getLastException();
        if (ex != null)
        {
            ex = ex.get_InnerException();
            if (ex != null)
            {
                error(ex.ToString());
            }
        }
        else
            throw error ("Error while connecting API");

    }

    streamReader.Close();
    dataStream.Close();
    response.Close();
}

But when I execute it i get the error below : 

pastedimage1635253744070v1.png

As you can see i'm not authorized to call it even though i'm passing my credentials.

I have tried to call the API from PostMan with NTLM authentication and the same credentials and it works fine.

Has anyone experienced similar issues? What could be the problem?

Looking forward to hear any advice,

kind regards

  • Dynamics 365 Profile Picture
    2 on at
    RE: WebAPI : NTLM authentication in Ax2012 R3

    could you please share the code

  • MCallebert Profile Picture
    45 on at
    RE: WebAPI : NTLM authentication in Ax2012 R3

    Hi Martin,

    Thank you for you answer it was very helpful.

    Now i'm able to call my API.

    Thank you a lot.

  • Verified answer
    Martin Dráb Profile Picture
    231,927 Most Valuable Professional on at
    RE: WebAPI : NTLM authentication in Ax2012 R3

    The framework supports working with credentials in an object-oriented way: use Credentials property of HttpWebRequest.

    For example, if you want to use credentials of the current user (note that it's different depending on whether you run your code on client or server), you can do something like this manually:

    request.set_Credentials(System.Net.CredentialCache::get_DefaultCredentials());

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,245 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,927 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans