使用Web API从JQuery请求令牌

Dan*_*-NP 5 authentication jquery jwt asp.net-web-api2

我在Javascript中执行ajax请求以从我的WebAPI AuthenticationProvider获取JWT.这是我的js函数:

    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;
}
Run Code Online (Sandbox Code Playgroud)

虽然内容以正确的方式发送给我,但OAuthValidateClientAuthenticationContext只有空值.

从控制台复制我的表单数据:

{ "grant_type": "密码", "CLIENT_ID": "myClientId", "client_secret": "myClientSecret", "用户名": "演示", "密码": "123"}

tg2*_*tg2 3

看起来你可能已经解决了这个问题,但是,这就是为我做的。使用上述内容并将主体设置为有效。

var body = {
    grant_type: 'password',
    username: credentials.name,
    password: credentials.password
};
Run Code Online (Sandbox Code Playgroud)