Home > Software design >  HTTP Error 400. The request hostname is invalid. On IIS when using `AllowedHosts` appsettings proper
HTTP Error 400. The request hostname is invalid. On IIS when using `AllowedHosts` appsettings proper

Time:03-07

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"
}

CodePudding user response:

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

CodePudding user response:

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();
    });
});
  • Related