Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request Token with JQuery from Web API

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"}

like image 888
Daniel Avatar asked Jul 09 '15 14:07

Daniel


1 Answers

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
};
like image 166
tg2 Avatar answered Oct 20 '22 16:10

tg2