'Why do I get the error: No 'Access-Control-Allow-Origin' header is present on the requested resource

I published a .Net6 application in a windows server's IIS with the address of http://tickettest.com and the application's program.cs contains the codes below:

builder.Services.AddCors(options =>
{
options.AddPolicy(name: "TicketPolicy",
                  policy =>
                  {
                      policy.AllowAnyOrigin();
                      policy.AllowAnyMethod();
                      policy.AllowAnyHeader();
                  });
});

//Other Codes Here

app.UseHttpsRedirection();

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

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
  ForwardedHeaders = ForwardedHeaders.XForwardedFor |
  ForwardedHeaders.XForwardedProto
});

Here is my controller as well:

[Route("api/V1/[controller]")]
[ApiController]
[EnableCors("TicketPolicy")]
[Authorize]
public class UserController : ControllerBase
{

    [HttpPost]
    [Route("Authenticate")]
    [AllowAnonymous]
    public JsonResult Authenticate([FromBody] AuthenticateModel model)
    {
      //This Method Authenticates and returns a JWT Token.
    }



    [HttpPost]
    [Route("Connect")]
    [AllowAnonymous]
    public JsonResult Connect([FromBody] AuthenticateModel model)
    {
        //This Method is for test only
        return new JsonResult(new { Result = false, MessageCode = 0 });
    }
}

It works fine when I send a request to the IIS Server and to the method, Connect. But, I get the error below from the Postman and the frontend when I want to send a request to Authenticate function!

Access to XMLHttpRequest at 'http://tickettest.com/api/V1/User/Authenticate' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. xhr.js?b50d:184 POST http://tickettest.com/api/V1/User/Authenticate net::ERR_FAILED 500


Error: http://tickettest.com/api/V1/User/Authenticate

Works fine: http://tickettest.com/api/V1/User/Connect


I would be appreciate it if you could help me with the issue.



Sources

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

Source: Stack Overflow

Solution Source