私は、ASP.NET Web API 2サービスへのサービス呼び出しを行っているAngular 2アプリケーションを開発中です。このサービスではCORSがそうのようWebApiConfig
で有効にされていますASP.NET Identity Tokenを使用したAngular 2ログイン
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
...
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
...
}
}
これは正常に動作してきたと私はCORSに問題がなかったしました。たとえば、/api/users
を呼び出すと、すべてのユーザーが正常に取得されます。
しかし、CORSに関連する問題を引き起こしているログイン機能に.NET IDトークンを使用しようとしています。私login.service.ts
でログインを実行するために/token
を呼び出す場合:
loginUser(username: string, password: string) {
let serviceURL = 'http://myurl/token';
let body = 'username=' + username + '&password=' + password + '&grant_type=password';
let headers: Headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options: RequestOptions = new RequestOptions({
headers: headers
});
return this.http.post(serviceURL, body, options)
.map(res => this.extractData(res))
.catch(this.handleError);
}
私は次のエラーを取得する:
XMLHttpRequest cannot load http://myurl/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 400.
私は二つの理由から、この混乱を見つけることだ:
- 上記のとおり、
/api
コールでCORS問題が発生していない - 正しい資格情報を入力すると、通話は「成功」(つまり、
Status Code 200
しかし、上記のエラーが表示されます。
私は同様の実装でさまざまな記事を見てきましたが、私は自分の間違いを見つけることができません。