Uri
Host
Authority
URL
Programming

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In .NET, Uri.Host and Uri.Authority are related but represent different parts of a URI. Confusing them can cause bugs in routing, logging, and security checks. The short version is that host is only the hostname part, while authority includes host plus optional port and user info semantics.

URI Component Breakdown

Given a URI like https://example.com:8443/path?q=1, components are:

  • scheme: https
  • authority: example.com:8443
  • host: example.com
  • port: 8443
  • path: /path

In .NET, properties expose these parts separately.

csharp
1using System;
2
3var uri = new Uri("https://example.com:8443/path?q=1");
4Console.WriteLine(uri.Host);      // example.com
5Console.WriteLine(uri.Authority); // example.com:8443
6Console.WriteLine(uri.Port);      // 8443

Authority is useful when host and port should be treated as one endpoint identifier.

Behavior with Default Ports

If URI uses default port for scheme, Authority may omit explicit port.

csharp
1var a = new Uri("https://example.com/path");
2Console.WriteLine(a.Host);      // example.com
3Console.WriteLine(a.Authority); // example.com
4Console.WriteLine(a.IsDefaultPort); // True

For non default port, authority includes it.

csharp
var b = new Uri("https://example.com:444/path");
Console.WriteLine(b.Authority); // example.com:444

This difference matters when building cache keys or origin comparisons.

IPv6 and Special Host Formats

With IPv6, host and authority formatting can differ in visible delimiters.

csharp
var uri = new Uri("http://[2001:db8::1]:8080/index");
Console.WriteLine(uri.Host);      // [2001:db8::1] or normalized form
Console.WriteLine(uri.Authority); // [2001:db8::1]:8080

Always parse with Uri instead of manual string splitting to avoid edge case mistakes.

Practical Usage Guidelines

Use Host when:

  • matching domain allowlists
  • grouping by domain only
  • certificate or DNS related logic

Use Authority when:

  • identifying full network endpoint
  • reconstructing base origin with port significance
  • comparing services behind different ports on same host

If you need scheme plus authority, combine with GetLeftPart.

csharp
var origin = uri.GetLeftPart(UriPartial.Authority);
Console.WriteLine(origin); // https://example.com:8443

Security and Validation Notes

Domain allowlists should usually compare normalized hostnames, not full authority strings, unless port is explicitly part of policy.

csharp
1var allowed = new[] { "api.example.com", "login.example.com" };
2bool ok = Array.Exists(allowed, h =>
3    string.Equals(h, uri.Host, StringComparison.OrdinalIgnoreCase)
4);

If policy depends on exact origin, compare scheme, host, and port explicitly rather than string contains checks.

Working with UriBuilder

When constructing endpoints dynamically, prefer UriBuilder rather than manual string concatenation. UriBuilder keeps host and port handling explicit, which reduces subtle bugs where ports are duplicated or accidentally dropped. It also helps normalize output when optional parts are absent.

csharp
1var builder = new UriBuilder();
2builder.Scheme = "https";
3builder.Host = "api.example.com";
4builder.Port = 8443;
5builder.Path = "v1/users";
6
7var uri = builder.Uri;
8Console.WriteLine(uri.Host);      // api.example.com
9Console.WriteLine(uri.Authority); // api.example.com:8443

Using this pattern makes later changes safer when endpoint composition rules evolve.

Internationalized domains can introduce normalization differences between user input and parsed host values. Normalize and log canonical URI components before security comparisons so incident debugging has one consistent representation across services.

Document chosen comparison rules so every service team applies URI checks consistently.

Review these rules regularly.

Common Pitfalls

  • Using Authority where only hostname comparison was intended and rejecting valid default port URIs.
  • Manually splitting URI strings instead of using parser properties.
  • Ignoring default port behavior and creating inconsistent cache keys.
  • Comparing authority values case sensitively when host matching should be case insensitive.
  • Treating path or query as part of authority when they are separate URI components.

Summary

  • Host is the hostname component of a URI.
  • Authority includes host plus port semantics for endpoint identity.
  • Default ports can make authority appear as host only.
  • Use built in Uri parsing to handle IPv6 and format edge cases.
  • Pick property based on whether your logic is domain level or endpoint level.

Course illustration
Course illustration

All Rights Reserved.