'HTTP Error 400. The request hostname is invalid. On IIS when using `AllowedHosts` appsettings property
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"
}
Solution 1:[1]
I'm stupid, I was trying to use AllowedHosts to configure Allowed Origins
Solution 2:[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();
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PancakeAlchemist |
| Solution 2 |
