C# Uri.Host 和 Uri.Authority 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2142910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:52:14  来源:igfitidea点击:

What's the difference between Uri.Host and Uri.Authority

c#.neturi

提问by Brian

System.Urihas Host, Authority, and DnsSafeHost. MS provides a nice example of when Hostand DnsSafeHostare different here.

System.UriHostAuthority、 和DnsSafeHost。MS 提供了一个很好的例子,说明何时HostDnsSafeHost这里不同。

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

我想要一个类似的例子/解释HostAuthority

采纳答案by Saurabh Mishra

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),

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

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

而主机名是域名或 IP 地址

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

  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

回答by kervin

From MSDN URI.Hostpage.

来自 MSDN URI.Host页面。

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

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

回答by Brandon

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

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

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

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

回答by Adrien

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

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

bob:[email protected]

鲍勃:[email protected]

more commonly used for FTP URIs

更常用于 FTP URI

回答by Steve Lautenschlager

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

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

Some examples of valid URIs:

一些有效 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));

According to RFC3986, Section 3.2the Authority contains

根据RFC3986第 3.2 节管理局包含

  1. User Information
  2. Host
  3. Port number.
  1. 用户信息
  2. 主持人
  3. 端口号。

NOT just host and port number.

不仅仅是主机和端口号。

For example, the following is a valid URI:

例如,以下是有效的 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.)

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

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 informationwhen returning the Authority which is rather annoying because it's not correct. Instead you can find the user info in the UserInfo property:

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

Uri.UserInfo

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

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

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

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

回答by Frank.Chang

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

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

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

where the authoritycomponent divides into three subcomponents:

其中权限组件分为三个子组件:

authority = [userinfo@]host[:port]

Like this:

像这样:

wiki

维基

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

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

  • An optional userinfosubcomponent 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 hostsubcomponent, 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 portsubcomponent preceded by a colon (:).
  • 一个可选的userinfo子组件,它可能包含一个用户名和一个可选的密码,前面是一个冒号 (:),后面是一个 at 符号 (@)。出于安全原因,不推荐在 userinfo 子组件中使用 username:password 格式。应用程序不应将 userinfo 子组件中第一个冒号 (:) 之后的任何数据呈现为明文,除非冒号之后的数据是空字符串(表示没有密码)。
  • 一个可选的主机子组件,由注册名称(包括但不限于主机名)或 IP 地址组成。IPv4 地址必须采用点十进制表示法,并且 IPv6 地址必须括在方括号 ([]) 中。
  • 以冒号 (:) 开头的可选端口子组件。

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

有关更多详细信息,您可以参考https://en.wikipedia.org/wiki/URL