I am hitting my WebAPI 2.0 Post method from Angular 5 component. The WebAPI has CORS enabled (shows in Response tab of Chrome), yet upon hitting it, shows as MethodNotAllowed. Not sure whats wrong in here. Authentication is using the bearer token. Postman hit is proper. Yet unable to hit the api when through angular
private PopulateHeaders() {
this.userIdAuthToken = localStorage.getItem(this.authToken);
let headers = new HttpHeaders({
'Content-Type': 'application/json'
});
// Once security is implemented, add the token here
if (this.userIdAuthToken)
headers.append('Authorization', 'Bearer ' + this.userIdAuthToken);
this.httpOptions = {
headers: headers
};
}
postProject(name: string, details: string): Observable<HttpEvent<Project>> {
var data = { name: name, details: details };
this.PopulateHeaders();
let saveRequest = this._http.post<Project>(
this.apiUrl + '/project' + '/post',
JSON.stringify(data),
this.httpOptions);
return saveRequest;
}
WebAPI code
[Authorize(Users = "admin@myapp.com")]
public int Post([FromBody]Project project)
{
lock (this)
{
project.Id = projects.Count() + 1;
projects.Add(project);
return project.Id;
}
}
(Just to mention) I am using OWIN authentication here, and my startup file has following:-
public void Configuration(IAppBuilder app)
{
// CORS has to be set first in order for it to be enabled for OWIN
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
this.ConfigureOAuthTokenGeneration(app);
this.ConfigureOAuthTokenConsumption(app);
this.ConfigureWebApi();
app.UseWebApi(HttpConfiguration);
}
If I have the "UseCors" call as first, the web api reports cors failure, If I add it to after oauth, it reports cors error on OAuth... Not sure where to set it up !!!