ASP.Net Core Web API unable to connect due to CORS

Hello, I want to call my POST-request through Swagger. Yet when I send the request it gives me the following message and shuts down the application; Undocumented Failed to fetch. Possible Reasons: CORS Network Failure URL scheme must be "http" or "https" for CORS request. I have tried to add CORS into my program.cs but to no avail. How can I solve this, so that I can communicate with my API?
4 Replies
Destination Unknown
c#
using Asp.Versioning;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));

builder.Services.AddControllers().AddJsonOptions(x =>
{
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services.AddProblemDetails();
builder.Services.AddApiVersioning(options =>
{
options.ReportApiVersions = true;
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
options.ApiVersionReader = new UrlSegmentApiVersionReader();
}).AddApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});

builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder
.WithOrigins("https://localhost:8080", "http://localhost:8081")
.AllowAnyHeader()
.AllowAnyMethod();
});
});


// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

if (app.Environment.IsStaging() || app.Environment.IsProduction())
{
app.UseHttpsRedirection();
}

app.UseCors("AllowAll");

app.UseAuthentication();

app.MapControllers();

#pragma warning disable S6966
app.Run();
#pragma warning restore S6966
c#
using Asp.Versioning;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));

builder.Services.AddControllers().AddJsonOptions(x =>
{
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services.AddProblemDetails();
builder.Services.AddApiVersioning(options =>
{
options.ReportApiVersions = true;
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
options.ApiVersionReader = new UrlSegmentApiVersionReader();
}).AddApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});

builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder
.WithOrigins("https://localhost:8080", "http://localhost:8081")
.AllowAnyHeader()
.AllowAnyMethod();
});
});


// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

if (app.Environment.IsStaging() || app.Environment.IsProduction())
{
app.UseHttpsRedirection();
}

app.UseCors("AllowAll");

app.UseAuthentication();

app.MapControllers();

#pragma warning disable S6966
app.Run();
#pragma warning restore S6966
appsettings.json: nothing really useful docker-compose.override:
services:
myProject.api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_HTTP_PORTS=8080
- ASPNETCORE_HTTPS_PORTS=8081
ports:
- "8080:8080"
- "8081:8081"
services:
myProject.api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_HTTP_PORTS=8080
- ASPNETCORE_HTTPS_PORTS=8081
ports:
- "8080:8080"
- "8081:8081"
docker-compose.yml
services:
myProject.api:
container_name: api
image: ${DOCKER_REGISTRY-}myProjectapi
build:
context: .
dockerfile: Sample.API/Dockerfile
ports:
- "8080:8080"
- "8081:8081"
services:
myProject.api:
container_name: api
image: ${DOCKER_REGISTRY-}myProjectapi
build:
context: .
dockerfile: Sample.API/Dockerfile
ports:
- "8080:8080"
- "8081:8081"
Without the CORS stuff, you can remove the following lines of the program.cs;
c#
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder
.WithOrigins("https://localhost:8080", "http://localhost:8081")
.AllowAnyHeader()
.AllowAnyMethod();
});
});

app.UseCors("AllowAll");
c#
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder
.WithOrigins("https://localhost:8080", "http://localhost:8081")
.AllowAnyHeader()
.AllowAnyMethod();
});
});

app.UseCors("AllowAll");
Anchy
Anchythis hour
I see you have setup your HTTP port as 8080 and your HTTPS port as 8081, however in your CORS settings you have reversed them
Destination Unknown
Hey Anchy, thank you for your reply. I would assume you mean this line of code:
.WithOrigins("https://localhost:8080", "http://localhost:8081")
.WithOrigins("https://localhost:8080", "http://localhost:8081")
I swapped the 2 but this still gave me the same 'error'
No description
Destination Unknown
But do i actually need CORS? Because as far as I know, CORS is used when you want to perform HTTP-requests to another domain. In my case everything is local(host). There is something else wrong. I do not get what it can be... it was the fockin browser. I used Brave for this, but brave blocked it. Using Edge works ARGH

Did you find this page helpful?