Uncategorized

cors for rest web api

to add support for CORS

  1. install package
    Install-Package Microsoft.AspNet.WebApi.Cors
  2. add to WebApiConfig.Register method
    config.EnableCors();
  3. To enable CORS for all Web API controllers in your application, pass an EnableCorsAttribute instance to the EnableCors method:
     var cors = new EnableCorsAttribute("www.example.com", "*", "*");

In the case you have token oauth authentication for rest web api, fix the pre-flight request cors issue:

  1. Move the code to the first line Startupapp.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
  2. REMOVE  context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); from the GrantResourceOwnerCredentials()

 

reference:

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s