'Access-Control-Allow-Origin despite Cors configured? [closed]

I am currently facing some issues regarding CORS, and constantly getting the classic error : "No 'Access-Control-Allow-Origin' header is present on the requested resource.".

I have tried configuring CORS in several ways, but none of them worked until now, and I'm running out of ideas on what to do. Could any of you help ?

Here is part of my Startup.cs :

private readonly string AllowAll = "_AllowAll";

public void ConfigureServices(IServiceCollection services)
    {
        _container = new UnityContainer();
        _services = services;

        services.AddCors(options =>
        {
            options.AddPolicy(name: AllowAll,
                                builder =>
                                {
                                    builder.AllowAnyOrigin()
                                            .AllowAnyMethod()
                                            .AllowAnyHeader();
                                });
        });

        // ...
        // More stuff here about configuring my UnitOfWork, Db, and such.
    }

    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();
        app.UseCors("AllowAll");

        app.UseAuthorization();

        // ...
        // Also some other unrelated configuration stuff
     }

And here is my Controller :

[Route("api")] 
public class UserController : ControllerBase
{
    private readonly IMapper _mapper;
    private readonly IUserService userService;

    public UserController(IMapper mapper, IUserService userService)
    {
        _mapper = mapper;
        this.userService = userService;
    }

    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [Route("connectUser")]
    [HttpGet]
    public IActionResult ConnectUser([FromQuery]string Username, string Password)
    {
        return Ok(userService.DoesUserExist(Username, Password));
    }
}

I can't understand what am I doing wrong. The API is correctly saying that it returns a success, so the issue isn't that an error isn't Cors-friendly. The Network tab on the browser displays for the Status : "Cross-origin Resource Sharing error: MissingAllowOriginHeader".

If there's any way I can give a bit more of precision or anything, please let me know !



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source