我正在使用Angular和.Net Web API 2后端进行身份验证。我的注册路由和其他资源正在运行,但登录/令牌不是。
在角度我的代码如下所示:
credentials.grant_type = "password";
credentials.userName = "email@email.com";
credentials.password = "asdfasdf";
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlenconded' });
let options = new RequestOptions({ headers: headers });
return this.http.post('http://localhost:58352/Token', credentials, options).map((response: Response) => {
return response.json();
});
但是,我收到了回复:
{"error":"unsupported_grant_type"}
在Angular.js(或Angular 1)中,我使用transformRequest使其正常工作。
答案 0 :(得分:0)
这就是诀窍!
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('userName', 'email@email.com');
urlSearchParams.append('password', 'asdfasdf');
urlSearchParams.append('grant_type', 'password');
let body = urlSearchParams.toString()
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlenconded' });
let options = new RequestOptions({ headers: headers });
return this.http.post('http://localhost:58352/Token', body, options).map((response: Response) => {
return response.json();
});