Get error about CORS in Web API 2.0

by February 21, 2016 0 comments
When working with Web API 2.0, I often get error about CORS. The problem occurred when you config wrong the package CORS which get from Nugget. Read many sources and questions to figure out which is the right configuration for this package. Finally, I got it. Just using these line of code in method Register of class WebApiConfig.cs

var cors = new EnableCorsAttribute("*", "*", "*");

config.EnableCors(cors);


No more using in IdentityConfig.cs, no more using in Provider/ApplicationOAuthProvider.cs. And no more using each class Controller Attribute. Of course this is used for global scope. Simply to manage.

Update one day later:

 One day later, got error CORS when get token. So also add this LOC into method GrantResourceOwnerCredentials in class Provider/ApplicationOAuthProvider.cs
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
Happy Coding!

An Nguyen

Developer

Sharing daily exciting lesson learned and posts about .NET, SQL Server, HTML, Javascript - jQuery, CSS - Bootstrap, SalesForce CRM, Kentico CMS and so on.

0 comments :

Post a Comment