7

I'm doing an ajax-request in Javascript to obtain a JWT from my WebAPI AuthenticationProvider. This is my js-function:

    function authenticateUser(credentials) {
    var body = {
        grant_type: 'password',
        client_id: 'myClientId',
        client_secret: 'myClientSecret',
        username: credentials.name,
        password: credentials.password
    };

    $.ajax({
        url: 'http://localhost:9000/token',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        /* data: JSON.stringify(body), /* wrong */
        data: body, /* right */
        complete: function(result) {
            //called when complete
            alert(result);
        },

        success: function(result) {
            //called when successful
            alert(result);
        },

        error: function(result) {
            //called when there is an error
            alert(result);
        },
    });
    session.isAuthenticated(true);
    return true;
}

Although the content is sent the right way in my eyes, the OAuthValidateClientAuthenticationContext has only null values.

My Form Data copied from the console:

{"grant_type":"password","client_id":"myClientId","client_secret":"myClientSecret","username":"demo","password":"123"}

3
  • your Ajax request looks similar to the ones I have used. Main difference I notice is stringifying the payload data. Do you have to do this? I never have. Commented Jul 9, 2015 at 15:07
  • That's the point. Thank you David. Commented Jul 10, 2015 at 6:03
  • no problem. Glad it was a simple change because it seemed like this was on the right track :) Commented Jul 10, 2015 at 14:57

1 Answer 1

4

Looks like you may have already solved it but, this is what did it for me. Using the above and setting body to this worked.

var body = {
    grant_type: 'password',
    username: credentials.name,
    password: credentials.password
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.