HTTP 错误 400。请求主机名无效。在 IIS 上使用“AllowedHosts” appsettings 属性时

发布于 2025-01-12 11:51:33 字数 879 浏览 2 评论 0原文

我收到错误 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掐死时间 2025-01-19 11:51:33

我很愚蠢,我试图使用 AllowedHosts 配置允许的来源

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

陌路黄昏 2025-01-19 11:51:33

您应该使用 , 拆分源变量中的 URL,然后在其余 CorePolicy 代码中使用它。

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

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();
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文