gpt4 book ai didi

c# - 主机与 DnsSafeHost

转载 作者:IT王子 更新时间:2023-10-29 03:59:04 26 4
gpt4 key购买 nike

我需要从 Request 对象中获取主机。我应该使用哪个属性,为什么?

来自 MSDN:

Uri.DnsSafeHost Property

A String that contains the unescaped host part of the URI that is suitable for DNS resolution; or the original unescaped host string, if it is already suitable for resolution.

对比

Uri.Host Property

A String that contains the host name. This is usually the DNS host name or IP address of the server.

我的测试是在 ASP.NET 开发服务器上进行的。这两个总是返回 localhost。即使我输入 127.0.0.1,两者都返回 localhost

继续阅读,DnsSafeHost 属性将处理 IPv6 地址,以及 Unicode 到 ASCII 的转换(如果需要)。它还可以考虑 IRI 和 IDN。即使我目前不关心这些事情,我是否应该只使用 DnsSafeHost 属性来确保安全?

最佳答案

如果您打算将生成的主机名发送到您自己的 DNS 服务器,请将 DnsSafeHost 应用于 URL。 DNS 最初是一种纯 ASCII 协议(protocol) ( and a subset of ASCII at that )。现代 URL 允许 URL 主机部分中的字符序列无法直接发送到 DNS 服务器。例子是:

  • bracket-enclosed IPv6 addresses . DnsSafeHost 去掉括号。

  • 非 ASCII、非英文域名。 DnsSafeHost(可选)使用 RFC 3987 IRI规范化字符,并将非 ASCII 字符转换为 IDN , Punycode编码。

  • 谁知道将来允许使用哪些 URL?该应用程序可能比我们对 URL 中可用内容的假设生命周期更长。 DnsSafeHost 增加了一定程度的弹性。

如果您需要向用户显示主机名,请改用Host 属性。

关于c# - 主机与 DnsSafeHost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1224587/

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