skip to Main Content

I get the error HTTP Error 400. The request hostname is invalid. on swagger when configuring CORS using the AllowedHost property.

var origins = Configuration["AllowedHosts"].Split(";");

services.AddCors(options =>
{
    options.AddDefaultPolicy(builder =>
    {
        builder.WithOrigins(origins)
            .AllowAnyHeader()
            .AllowAnyMethod();
    });
});

But if I use a different property like Test swagger gets loaded fine.

var origins = Configuration["Test"].Split(";");

appsettings.Development.json

{
 "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "http://localhost:3000;https://localhost:44308"
}

2

Answers


  1. Chosen as BEST ANSWER

    I'm stupid, I was trying to use AllowedHosts to configure Allowed Origins


  2. You should split your URLs in origin’s variable with , then use it in your rest CorePolicy code.

    services.AddCors(options =>
    {
        options.AddDefaultPolicy(builder =>
        {
            builder.WithOrigins(String.Join(",", origins))
                .AllowAnyHeader()
                .AllowAnyMethod();
        });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search