Share via

SharePoint 2013 Rest API Authentication Problem

Anonymous
2016-06-28T11:05:18+00:00

Hi,

We created SharePoint site (Windows Authentication). We developed mobile app and tried to connect to the site by using Rest API that already created by MS SharePoint. We created the below functions to authenticate mobile app requests and it's working well but we face a problem because Rest API requested authentication each time we make a request so we resolved this issue by calling these functions many times. The final result is performance is bad.

Note: We use ntlm.js library

I need to authenticate once.

JavaScript****Functions:

Ntlm.setCredentials = function (domain, username, password) {

    var magic = 'KGS!@#$%'; // Create LM password hash.

    var lmPassword = password.toUpperCase().substr(0, 14);

    while (lmPassword.length < 14) lmPassword += '\0';

    var key1 = Ntlm.createKey(lmPassword);

    var key2 = Ntlm.createKey(lmPassword.substr(7));

    var lmHashedPassword = des(key1, magic, 1, 0) + des(key2, magic, 1, 0);

    var ntPassword = ''; // Create NT password hash.

    for (var i = 0; i < password.length; i++)

        ntPassword += password.charAt(i) + '\0';

    var ntHashedPassword = str_md4(ntPassword);

    Ntlm.domain = domain;

    Ntlm.username = username;

    Ntlm.lmHashedPassword = lmHashedPassword;

    Ntlm.ntHashedPassword = ntHashedPassword;

};

Ntlm.authenticate = function (url, Method) {

    if (!Ntlm.domain || !Ntlm.username || !Ntlm.lmHashedPassword || !Ntlm.ntHashedPassword) {

        Ntlm.error('No NTLM credentials specified. Use Ntlm.setCredentials(...) before making calls.');

        return false;

    }

    var hostname = Ntlm.getLocation(url).hostname;

    var msg1 = Ntlm.createMessage1(hostname);

    var request = new XMLHttpRequest();

    request.open(Method, url, false);

    request.setRequestHeader('Authorization', 'NTLM ' + msg1.toBase64());

    request.overrideMimeType('text/xml; charset=UTF-8');

    request.send();

    var response = request.getResponseHeader('WWW-Authenticate');

    var challenge = Ntlm.getChallenge(response);

    var msg3 = Ntlm.createMessage3(challenge, hostname);

    request.open(Method, url, false);

    request.overrideMimeType('text/xml; charset=UTF-8');

    request.setRequestHeader('Authorization', 'NTLM ' + msg3.toBase64());

    request.send();

    return request.status == 200;

};

Regards,

Khaled Abed

Microsoft 365 and Office | SharePoint | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2016-06-28T16:19:43+00:00

    Hi Khaled,

    We’d suggest you post the issue in our SharePoint 2013 development forum which is a specific support channel for development and programming related issues. Thanks for your understanding.

    Regards,

    Jiaxing Bian

    Was this answer helpful?

    0 comments No comments