Request Token with JQuery from Web API

时光怂恿深爱的人放手 提交于 2019-12-04 12:57:28

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/31321182/request-token-with-jquery-from-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!