gpt4 book ai didi

c# - appsettings.json 中的 AllowedHosts 和 .NET Core API 3.x 中的 UseCors 之间的区别

转载 作者:行者123 更新时间:2023-12-03 05:54:32 26 4
gpt4 key购买 nike

我看到 .NET Core 3.x 带有一个新的特殊配置 允许的主机 用于列出允许访问该站点的主机,而 CORS (app.UseCors) 已存在此选项。
这两个选项有什么区别?

最佳答案

根据 documentation , allowedHosts用于主机过滤以将您的应用绑定(bind)到特定主机名。例如,如果您替换以下内容:

"AllowedHosts": "*"
"AllowedHosts": "example.com"
并且您尝试使用 http://localhost:xxxx/ 访问您的应用程序地址你会得到默认的错误请求(400)响应。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML>

<HEAD>
<TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii">
</ HEAD>

<BODY>
<h2>Bad Request - Invalid Hostname</h2>
<hr>
<p>HTTP Error 400. The request hostname is invalid.</p>
</BODY>

</HTML>
由于主机过滤中间件不允许应用程序将应用程序绑定(bind)到除 example.com 之外的任何其他主机名.
CORS
另一方面,CORS 是控制哪些主机尝试访问您的应用程序上的资源 (API)。

关于c# - appsettings.json 中的 AllowedHosts 和 .NET Core API 3.x 中的 UseCors 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59750012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com