For "C# - ASP Net Core Web API (Net Core 3.1 LTS)", it worked for me ...
In Startup.cs file:
Inside "ConfigureServices" function add this code:
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
Note: In case of "CorsPolicy" you can change for what you like or use global variable in "Startup" class.
Inside "Configure" function add this code:
app.UseCors("CorsPolicy");
Check the call order of the functions, it should look like this:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("CorsPolicy");
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
And finally in your controller class add this code above your functions http:
[EnableCors("CorsPolicy")]
For example:
[EnableCors("CorsPolicy")]
[HttpPost("UserLoginWithGoogle")]
public async Task<ActionResult<Result>> UserLoginWithGoogle([FromBody] TokenUser tokenUser)
{
Result result = await usersGoogleHW.UserLoginWithGoogle(tokenUser.Token);
return new JsonResult(result);
}
Note: "CorsPolicy" must match in the Startup and Controller.
Good luck ...