HTTP 错误 400。请求主机名无效。在 IIS 上使用“AllowedHosts” appsettings 属性时
我收到错误 HTTP 错误 400。请求主机名无效。使用AllowedHost 属性配置CORS 时,会出现在swagger 上。
var origins = Configuration["AllowedHosts"].Split(";");
services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder.WithOrigins(origins)
.AllowAnyHeader()
.AllowAnyMethod();
});
});
但是,如果我使用不同的属性,例如 Test
swagger 则可以正常加载。
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"
}
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"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很愚蠢,我试图使用 AllowedHosts 配置允许的来源
I'm stupid, I was trying to use AllowedHosts to configure Allowed Origins
您应该使用
,
拆分源变量中的 URL,然后在其余 CorePolicy 代码中使用它。You should split your URLs in origin's variable with
,
then use it in your rest CorePolicy code.