web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,882 Most Valuable Professional on at

    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());

  • MCallebert Profile Picture
    45 on at

    Hi Martin,

    Thank you for you answer it was very helpful.

    Now i'm able to call my API.

    Thank you a lot.

  • Dynamics 365 Profile Picture
    6 on at

    could you please share the code

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 584 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 499 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 254 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans