从 Web API 使用 JQuery 请求令牌 [英] Request Token with JQuery from Web API

查看:26
本文介绍了从 Web API 使用 JQuery 请求令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

推荐答案

看起来你可能已经解决了,但是,这就是为我解决的问题.使用上述方法并将正文设置为此有效.

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

这篇关于从 Web API 使用 JQuery 请求令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆