从Web API使用JQuery请求令牌 [英] Request Token with JQuery from Web API
问题描述
我正在用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":密码","client_id":"myClientId","client_secret":"myClientSecret",用户名":"demo",密码":"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屋!