Problem with Cors C# Asp. Net

Asked

Viewed 381 times

0

I’m having enough trouble solving a CORS error.

I am following the following steps:

  • I added the CORS Nuget package to my project.
  • I’m adding the code config.EnableCors(); in my class startUp.
  • I added the attribute [EnableCors] to class Controller.

even following these steps I keep getting error from CORS

Access to Xmlhttprequest at 'http://xxx.xxx.x.xxx/api/meuprojeto/Singup/' from origin 'https://xxx.xxx.x.xxx' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains Multiple values '*, *', but only one is allowed.

Startup Code:

 public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        var formatters = config.Formatters;
        formatters.Remove(formatters.XmlFormatter);

        var jsonSettings = formatters.JsonFormatter.SerializerSettings;
        jsonSettings.Formatting = Formatting.Indented;
        jsonSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

        formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        config.MapHttpAttributeRoutes();

        //WEB API configuration and services 
        config.EnableCors();

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

        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);
    }

My controller:

namespace OdyssAppws.ControllerOdyss

    {
        [EnableCors(origins: "https://xxx.xxx.x.xxx/", headers: "*", methods: "*")]
        [RoutePrefix("api/meuprojeto")]
        public class IdUserController : ApiController
        {
  • Dude, I don’t think you need to put config.enablecors and app.usecors, just leave the app.usecors and try again

  • @Lucasmiranda continues pointing Cors error

  • Access to Xmlhttprequest at 'http://xxx.xxx.x.xxx/Singup/' from origin 'https://xxx.xxx.x.xxx' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested source.

  • Try config.EnableCors(new EnableCorsAttribute(Properties.Settings.Default.Cors, "", "")) and remove the attribute.

  • @Barbetta Hello, sorry, it was not very clear to me Remove that attribute ?

  • Remove the attribute EnableCors from above the controller

  • @Barbetta without success :/

  • The problem may be in the way I am making the request for my api ?

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.