✅ Cors Errors on new API controller

Im getting CORS errors on my new Fund controller even though the other Controllers are still working.
No description
9 Replies
restingphantom
restingphantomOP9mo ago
my program.cs looks as followed
C#
global using HTXL_Back_end.Models;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;

var corsPolicy = "CorsPolicy";

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

// Authentication
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey =
new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(builder.Configuration.GetSection("AppSettings:Token").Value!)),
ValidateIssuer = false,
ValidateAudience = false
};
});

// CORS
builder.Services.AddCors(options =>
options.AddPolicy(corsPolicy, policy => policy
.WithOrigins(builder.Configuration.GetSection("AppSettings:AllowedOrigins").Get<string[]>()!)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()));

builder.Services.AddDbContext<HTXLPortfolioContext>();

var app = builder.Build();

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

app.UseCors(corsPolicy);

app.UseHttpsRedirection();

app.UseAuthentication();

app.UseAuthorization();

app.MapControllers();

app.Run();
C#
global using HTXL_Back_end.Models;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;

var corsPolicy = "CorsPolicy";

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

// Authentication
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey =
new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(builder.Configuration.GetSection("AppSettings:Token").Value!)),
ValidateIssuer = false,
ValidateAudience = false
};
});

// CORS
builder.Services.AddCors(options =>
options.AddPolicy(corsPolicy, policy => policy
.WithOrigins(builder.Configuration.GetSection("AppSettings:AllowedOrigins").Get<string[]>()!)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()));

builder.Services.AddDbContext<HTXLPortfolioContext>();

var app = builder.Build();

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

app.UseCors(corsPolicy);

app.UseHttpsRedirection();

app.UseAuthentication();

app.UseAuthorization();

app.MapControllers();

app.Run();
the CORS error dont occur in my dev env. only in production
Pobiega
Pobiega9mo ago
and whats in your Appsettings:AllowedOrigins during production? I also dont see anything about UseStaticFiles, so I'm assuming the frontend is hosted elsewhere
restingphantom
restingphantomOP9mo ago
The front-end is hosted somewhere else, the env variables during production are https://HOST.net and http://HOST.net at least I believe them to be, because i unfortionately dont have access to them
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
restingphantom
restingphantomOP9mo ago
thats where my api is when running loccally!? I didnt even notice that
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
restingphantom
restingphantomOP9mo ago
I found it already
restingphantom
restingphantomOP9mo ago
I already had this error before
No description
restingphantom
restingphantomOP9mo ago
it auto implements from envoirement.dev instead of just envoirenment thanks for the help though

Did you find this page helpful?