2017-03-15 67 views
-1

我正在使用Angular和.Net Web API 2後端進行身份驗證。我的註冊路線和其他資源正在運行,但登錄/令牌不是。Angular和Asp.net Web Api 2令牌

在郵遞員,這要求工作,我得到令牌回來:

在角我的代碼如下所示:

credentials.grant_type = "password"; 
    credentials.userName = "[email protected]"; 
    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

這個伎倆!

let urlSearchParams = new URLSearchParams(); 
    urlSearchParams.append('userName', '[email protected]'); 
    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(); 
    });