Uri.Host 和 Uri.Authority 之间有什么区别

发布于 2024-08-19 23:01:57 字数 344 浏览 4 评论 0原文

System.Uri 具有 HostAuthorityDnsSafeHost。 MS 提供了一个很好的示例,说明 HostDnsSafeHost 何时不同此处

我想要 HostAuthority 的类似示例/解释。

System.Uri has Host, Authority, and DnsSafeHost. MS provides a nice example of when Host and DnsSafeHost are different here.

I'd like a similar example/explanation for Host and Authority.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

败给现实 2024-08-26 23:01:57

是的,布兰登是绝对正确的,用外行人的话说,

权限=主机名+端口号

,如果URL协议使用默认端口,比如http URL的端口80,那么只有在这种情况下
权限 = 主机名(假设端口号为 80),

而主机名可以是域名或 IP 地址

示例:

  1. http://www.example.com/< /代码>

    权限 = www.example.com
    主机名= www.example.com

  2. http://255.255.255.255:8080/

    权限= 255.255.255.255:8080
    主机名 = 255.255.255.255

Yes Brandon is absolutely correct, in layman terms

Authority = Host Name + Port No

And if URL protocol is using a default port, say port 80 for http URL, then only in that case
Authority = Host Name (Port No is assumed to be 80),

Whereas Host Name is either Domain Name or I.P Address

Example:

  1. http://www.example.com/

    Authority = www.example.com
    Host Name = www.example.com

  2. http://255.255.255.255:8080/

    Authority = 255.255.255.255:8080
    Host Name = 255.255.255.255

习ぎ惯性依靠 2024-08-26 23:01:57

来自 MSDN URI.Host 页面。

与 Authority 属性不同,此属性值不包括端口号。

From MSDN URI.Host page.

Unlike the Authority property, this property value does not include the port number.

情未る 2024-08-26 23:01:57

每个 HTTP URL 都符合通用 URI 的语法。 URI 通用语法由五个组件的分层序列组成:

URI = scheme:[//authority]path[?query][#fragment]

其中 authority 组件分为三个子组件:

authority = [userinfo@]host[:port]

像这样:

wiki

一个可选的 authority 组件,前面有两个斜杠 (//),包括:

  • 可选的 userinfo 子组件,可以包含用户名和可选密码,前面是冒号 (:),后跟 at 符号 (@)。出于安全原因,不建议在 userinfo 子组件中使用用户名:密码格式。应用程序不应将用户信息子组件中第一个冒号 (:) 之后的任何数据呈现为明文,除非冒号之后的数据是空字符串(表示没有密码)。
  • 可选的主机子组件,由注册名称(包括但不限于主机名)或IP地址组成。 IPv4 地址必须采用点十进制表示法,IPv6 地址必须括在方括号 ([]) 中。
  • 可选的端口子组件,前面带有冒号 (:)。

欲了解更多详情,您可以参考https://en.wikipedia.org/wiki/URL

Every HTTP URL conforms to the syntax of a generic URI. The URI generic syntax consists of a hierarchical sequence of five components:

URI = scheme:[//authority]path[?query][#fragment]

where the authority component divides into three subcomponents:

authority = [userinfo@]host[:port]

Like this:

wiki

An optional authority component preceded by two slashes (//), comprising:

  • An optional userinfo subcomponent that may consist of a user name and an optional password preceded by a colon (:), followed by an at symbol (@). Use of the format username:password in the userinfo subcomponent is deprecated for security reasons. Applications should not render as clear text any data after the first colon (:) found within a userinfo subcomponent unless the data after the colon is the empty string (indicating no password).
  • An optional host subcomponent, consisting of either a registered name (including but not limited to a hostname), or an IP address. IPv4 addresses must be in dot-decimal notation, and IPv6 addresses must be enclosed in brackets ([]).
  • An optional port subcomponent preceded by a colon (:).

For more details, you can refer to https://en.wikipedia.org/wiki/URL .

以酷 2024-08-26 23:01:57

对于.NET中的Uri类,Authority包含端口,Host不包含端口,也不包含用户信息。

有效 URI 的一些示例:

Uri u = new Uri("http://www.domain.com/path");
Assert.AreEqual("www.domain.com", u.Authority);
Assert.AreEqual("www.domain.com", u.Host);
Assert.AreEqual("http://www.domain.com", u.GetLeftPart(UriPartial.Authority));

u = new Uri("http://www.domain.com:8080/path");
Assert.AreEqual("www.domain.com:8080", u.Authority);
Assert.AreEqual("www.domain.com", u.Host);
Assert.AreEqual("http://www.domain.com:8080", u.GetLeftPart(UriPartial.Authority));

u = new Uri("http://user:password@host:555/path");
Assert.AreEqual("host:555", u.Authority);
Assert.AreEqual("host", u.Host);
Assert.AreEqual("http://user:password@host:555", u.GetLeftPart(UriPartial.Authority));

根据 RFC3986第 3.2 节 授权机构包含

  1. 用户信息
  2. 主机
  3. 端口号。

不仅仅是主机和端口号。

例如,以下是有效的 URI:

http://user:password@host:80/path

其中权限为

user:password@host:80

at 符号 (@) 将用户信息与主机分隔开,冒号 (:) 将主机与端口号分隔开。在用户信息中,冒号 (:) 将用户名与密码分隔开。 (是的,我知道密码部分已被弃用。它仍然可以选择支持。)

这是权威的完整规范。显然,用户信息和端口号通常不存在。

.NET 中的 Uri 类在返回权限时会删除用户信息,这是相当烦人的,因为它不正确。相反,您可以在 UserInfo 属性中找到用户信息:

Uri.UserInfo

其他答案在技术上是正确的,即 对于 .NET Uri 类 Uri.Authority 和 Uri 之间的差异。 Host是主机不会包含端口号。

但请注意,Authority 未按照在 .NET Uri 类中使用的方式正确定义,因为它还可能包含用户信息。

For the Uri class in .NET, Authority includes the port, Host does not, and neither includes user information.

Some examples of valid URIs:

Uri u = new Uri("http://www.domain.com/path");
Assert.AreEqual("www.domain.com", u.Authority);
Assert.AreEqual("www.domain.com", u.Host);
Assert.AreEqual("http://www.domain.com", u.GetLeftPart(UriPartial.Authority));

u = new Uri("http://www.domain.com:8080/path");
Assert.AreEqual("www.domain.com:8080", u.Authority);
Assert.AreEqual("www.domain.com", u.Host);
Assert.AreEqual("http://www.domain.com:8080", u.GetLeftPart(UriPartial.Authority));

u = new Uri("http://user:password@host:555/path");
Assert.AreEqual("host:555", u.Authority);
Assert.AreEqual("host", u.Host);
Assert.AreEqual("http://user:password@host:555", u.GetLeftPart(UriPartial.Authority));

According to RFC3986, Section 3.2 the Authority contains

  1. User Information
  2. Host
  3. Port number.

NOT just host and port number.

For example, the following is a valid URI:

http://user:password@host:80/path

in which the Authority is

user:password@host:80

The at symbol (@) delimits the user info from the host and the colon (:) delimits the host from the port number. Within the user info, a colon (:) delimits the username from the password. (Yes, I know the password portion is deprecated. It may still optionally be supported.)

This is the full spec for an Authority. Obviously, the user info and port number are often not present.

The Uri class in .NET drops the user information when returning the Authority which is rather annoying because it's not correct. Instead you can find the user info in the UserInfo property:

Uri.UserInfo

Other answers are technically correct to say that for the .NET Uri class that the difference between Uri.Authority and Uri.Host is that the host will not contain a port number.

But please know that Authority is not properly defined the way it is used in the .NET Uri class because it may also contain user info.

合久必婚 2024-08-26 23:01:57

根据您链接到的文档,如果 Authority 属性与 Uri 的默认端口不同,则该属性将包含端口号,而 Host 属性仅包含端口号返回 DNS 主机名或 IP 地址。

我认为没有比这更多的差异了。

According to the documentation you linked to, the Authority property will include the port number if it is not the same as the default port of the Uri, while the Host property will only return the DNS Host name or IP Address.

I don't believe there are any more differences than that.

谎言月老 2024-08-26 23:01:57

权限还可以包含用户名和密码,例如

bob:[电子邮件受保护]

更常用于 FTP URI

Authority can also include a username and password, e.g.

bob:[email protected]

more commonly used for FTP URIs

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