I recently moved from my IISExpress development server to IIS 8.5. Now I am getting this CORS error. All configuration has been set correctly in the code, ASP.NET Core.
Here is my program.cs
and my controller:
using Microsoft.AspNetCore.Authentication.Negotiate;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using WebApplication2.App_Contexts;
var builder = WebApplication.CreateBuilder(args);
ILoggingBuilder loggingBuilder = builder.Logging.AddConsole();
loggingBuilder.SetMinimumLevel(LogLevel.Debug);
// Add services to the container.
builder.Services.AddCors(opt =>
{
opt.AddDefaultPolicy( policy =>
{
policy.WithOrigins("http://localhost:3000")
.AllowAnyHeader()
.AllowCredentials()
.AllowAnyMethod();
});
});
builder.Services.AddControllers();
builder.Services.AddDbContext<DEV_IntranetContext>();
builder.Services.AddDbContext<PreAuthContext>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
builder.Services.AddAuthorization(options =>
{
// By default, all incoming requests will be authorized according to the default policy.
options.FallbackPolicy = options.DefaultPolicy;
});
var app = builder.Build();
app.UseStaticFiles();
//app.UseHttpsRedirection();
app.UseCors("CORS");
app.Use(async (context, next) =>
{
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
context.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
if (context.Request.Method == "OPTIONS")
{
context.Response.StatusCode = (int)HttpStatusCode.OK;
await context.Response.WriteAsync("OK");
return;
}
await next();
});
app.UseAuthentication();
app.UseAuthorization();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
AppDomain.CurrentDomain.SetData("ContentRootPath", builder.Environment.ContentRootPath);
AppDomain.CurrentDomain.SetData("WebRootPath", builder.Environment.WebRootPath);
app.MapControllers();
app.Run();
SitemapController.cs
:
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WebApplication2.App_Contexts;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using WebApplication2.App_Linq;
namespace MCA.Controllers
{
[EnableCors("AllowAllOrigins")]
[ApiController]
[Route("[controller]")]
public class SitemapController : ControllerBase
{
private readonly DEV_IntranetContext _db;
public SitemapController(DEV_IntranetContext db)
{
_db = db;
}
[EnableCors("AllowAllOrigins")]
[HttpGet]
public async Task<string> Get()
{
var map = Data.Sitemap(_db);
var sitemap = JArray.Parse(map);
string? name = MCA.APIUtilities.GetName(HttpContext);
if (name is not null)
{
var user = await new User(name, _db).Initialize();
var roles = user.Roles.ToList();
var data = new
{
sitemap,
user = new
{
user = user.Name,
roles = string.Join(",", roles).ToLower()
}
};
var ret = JsonConvert.SerializeObject(data);
return ret;
}
else
{
var data = new
{
sitemap,
user = ""
};
return JsonConvert.SerializeObject(data);
}
}
}
}
I have gone through every tutorials in page I can find. So far as I can tell the code is set up correctly. The calling app is React, but curl doesn’t succeed either.
Thanks
8