Panu Oksala
Panu Oksala

Reputation: 3438

Web API CORS support not working on http

I have enabled CORS support on my Web API controller and it works fine on https, but on http I'm still getting this o 'Access-Control-Allow-Origin' header is present on the requested resource error.

In WebApiConfig I have enabled cors by using this line:

// Allow cross site javascript calls
config.EnableCors();

And my controller class is introduced like this:

 [EnableCors("*","*","*")]
 public class ExternalDataController : ApiController    

So how I can enable CORS on http and https?

On azure raw http logs following lines appear when I create a one call:

2015-05-06 05:53:15 ~1PROJECTX GET /diagnostics/settings X-ARR-LOG-ID=d5d487c0-8d32-4103-98cb-37fd7b022942 443 - 137.116.210.73 Portal-Exp/5.11.2.704+(Websites)+Mozilla/5.0+(Windows+NT+6.3;+WOW64)+AppleWebKit/537.36+(KHTML%2C+like+Gecko)+Chrome/42.0.2311.90+Safari/537.36+OPR/29.0.1795.47 - - projectx-development.scm.azurewebsites.net 200 0 0 733 1215 3921
2015-05-06 05:53:20 ~1PROJECTX POST /diagnostics/settings X-ARR-LOG-ID=599cb771-d399-49ee-89a2-6804af7c2712 443 - 137.116.210.73 Portal-Exp/5.11.2.704+(Websites)+Mozilla/5.0+(Windows+NT+6.3;+WOW64)+AppleWebKit/537.36+(KHTML%2C+like+Gecko)+Chrome/42.0.2311.90+Safari/537.36+OPR/29.0.1795.47 ARRAffinity=a32808ec0f09a64a5ce1705808bf8a2fb0c447c78db8498153641c9469a96a96 - projectx-development.scm.azurewebsites.net 204 0 0 500 1497 156
2015-05-06 05:53:21 ~1PROJECTX GET /diagnostics/settings X-ARR-LOG-ID=6d7bcfd4-aeeb-4c02-9f02-cf5bd54d2189 443 - 137.116.210.73 Portal-Exp/5.11.2.704+(Websites)+Mozilla/5.0+(Windows+NT+6.3;+WOW64)+AppleWebKit/537.36+(KHTML%2C+like+Gecko)+Chrome/42.0.2311.90+Safari/537.36+OPR/29.0.1795.47 ARRAffinity=a32808ec0f09a64a5ce1705808bf8a2fb0c447c78db8498153641c9469a96a96 - projectx-development.scm.azurewebsites.net 200 0 0 732 1301 78

Upvotes: 1

Views: 338

Answers (2)

Dunken
Dunken

Reputation: 8681

Do you use OWIN? Just enable CORS in your Configuration method:

    public void Configuration(IAppBuilder app)
    {
        #region CORS

        app.UseCors(CorsOptions.AllowAll); 

        #endregion 

Upvotes: 1

codebased
codebased

Reputation: 7073

Sorry I have not checked at Controller level.

I have the following at WebApi config level and it is working as expected.

 var cors = new EnableCorsAttribute("*", "*", "*") ;
            config.EnableCors(cors);

Similarly if you are using Basic auth then make sure that you set SupportsCredentials value as well

        var cors = new EnableCorsAttribute("*", "*", "*") { SupportsCredentials = true };

Upvotes: 0

Related Questions