Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

WebAPI : NTLM authentication in Ax2012 R3

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
    Dynamics 365 2 on at
    RE: WebAPI : NTLM authentication in Ax2012 R3

    could you please share the code

  • MCallebert Profile Picture
    MCallebert 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
    Martin Dráb 230,214 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans