chaitu301
chaitu301

Reputation: 69

CORS issue on chrome & firefox

The web API from IIS 7.5 are not responding for Chrome & Firefox. I am getting the following error in chrome No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://10.xx.xx.xx:81' is therefore not allowed access

Firefox throw 401 unauthorized error.

Works perfectly on IE 11

Are there any additional setting required for these browsers?

Upvotes: 2

Views: 1376

Answers (2)

Federico Dipuma
Federico Dipuma

Reputation: 18265

First install WebApi Cors NuGet Package:

Install-Package Microsoft.AspNet.WebApi.Cors

Then in your WebApiConfig.cs file:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        var cors = new EnableCorsAttribute("*", "*", "*");
        config.EnableCors(cors);

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        //other code
    }
}

This way you enable CORS header globally allowing cross-site requests from any domain. If you want to allow cross-site requests from only a single (or a list of) domain, then change the constructor parameters of EnableCorsAttribute:

new EnableCorsAttribute("http://alloweddomain.com", "*", "*");

You may also apply the EnableCorsAttribute on a Controller or Action basis.

More information on the official documentation: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

Upvotes: 1

user1398619
user1398619

Reputation: 696

"A resource makes a cross-origin HTTP request when it requests a resource from a different domain than the one which the first resource itself serves" You can use a chrome addon to allow CORS:

https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

Upvotes: 0

Related Questions