110

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.

6 Answers 6

137

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

6
  • 1
    One small doubt. if I run the application in localhost I have port number in my URL but in production I do not have port number in the URL. So, I can freely use Url.Authority right? I mean if port number is not there then URl.Authority will act same as URL.Host right. I mean I will still be able to get the URL correctly right? please guide me,. Dec 28, 2016 at 16:04
  • Read stackoverflow.com/a/39415643/316343, it missed the User Information part. Apr 9, 2017 at 23:56
  • Is there an documentation reference for this answer? Something authoritative?
    – Jonathan M
    Dec 8, 2020 at 17:45
  • @JonathanM Yes, see learn.microsoft.com/en-us/dotnet/api/system.uri.authority
    – Peter B
    May 14, 2021 at 9:23
  • This term should not be used in this context. This term is not popular, not contextually intuitive, and has implications that do not lend itself to making sense. "Authority" implies a name of some established large organization acting as a third party to this exchange. We already have enough random terms, so someone needs to put a stop to this. A domain + a port already has a name: "domain" or "service". The port is implied in the domain part of a URL, even if the technical definition of 'domain' only includes the name.
    – Ryan
    Jun 5 at 2:30
39

From MSDN URI.Host page.

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

2
  • One small doubt. if I run the application in localhost I have port number in my URL but in production I do not have port number in the URL. So, I can freely use Url.Authority right? I mean if port number is not there then URl.Authority will act same as URL.Host right. I mean I will still be able to get the URL correctly right? please guide me. Dec 28, 2016 at 16:04
  • @Unbreakable - That's right it'll work for both cases. When you don't specify the port number in the url you just use the default one and in that case Url.Authority omits the port.
    – BornToCode
    May 31, 2018 at 8:17
29

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 .

18

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.

0
11

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.

1
  • 1
    One small doubt. if I run the application in localhost I have port number in my URL but in production I do not have port number in the URL. So, I can freely use Url.Authority right? I mean if port number is not there then URl.Authority will act same as URL.Host right. I mean I will still be able to get the URL correctly right? please guide me. Dec 28, 2016 at 16:04
0

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

bob:pwd@somewhere.example.com

more commonly used for FTP URIs

3
  • sorry, you're wrong. From RFC 3986 section 3.2 Authority "authority = [ userinfo "@" ] host [ ":" port ]"
    – Adrien
    Aug 16, 2016 at 13:59
  • user info with plain passwords is deprecated in the RFC but still in common use, especially with FTP URIs, and IE still supports this (in fact it's the only way to auth with FTP via IE through an http proxy).
    – Adrien
    Aug 16, 2016 at 14:02
  • 4
    The question is about System.URI.Authority, which is NOT the same as RFC 3986.
    – Brian
    Sep 9, 2016 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.