According to RFC3986, "authority" part is optional, however, if it is present, "host" is mandatory:
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
hier-part = "//" authority path-abempty
/ path-absolute
/ path-rootless
/ path-empty
authority = [ userinfo "@" ] host [ ":" port ]
So, if you set "host" to an empty string, "userinfo" and "port" can't bet set.
I'm not familiar with LDAP, so it isn't clear what URI you're trying to produce. If port and username are set to non-empty strings, but host is empty, what URI is expected to look like?
.NET explicitly supports LDAP URIs. This support boils down to these flags:
private const UriSyntaxFlags LdapSyntaxFlags =
UriSyntaxFlags.MustHaveAuthority |
//
UriSyntaxFlags.AllowEmptyHost |
UriSyntaxFlags.AllowUncHost |
UriSyntaxFlags.AllowAnInternetHost |
//
UriSyntaxFlags.MayHaveUserInfo |
UriSyntaxFlags.MayHavePort |
UriSyntaxFlags.MayHavePath |
UriSyntaxFlags.MayHaveQuery |
UriSyntaxFlags.MayHaveFragment |
//
UriSyntaxFlags.PathIsRooted |
UriSyntaxFlags.AllowIdn |
UriSyntaxFlags.AllowIriParsing;
UriBuilder
, when constructed this way:
var ub = new UriBuilder {
Scheme = "ldap", Host = "", Path = "/cn=...", UserName = "u", Password = "p", Port = 100
};
produces this string when ToString()
is called:
ldap://u:p@/cn=...
However, creating Uri
with this string will fail. I guess it can be considered a limitation. It's not the first case of Uri
's limitations. For example, it enforces parsing of paths in HTTP URIs as Windows paths, which can be annoying and cause bugs in some cases.
If what you are actually interested about is LDAP URIs, maybe you should just read documentation for Active Directory. The simplest URI is just a scheme + some string, the rest is left for URI implementation to decide, the standard doesn't enforce anything. So even if some URIs follow some part of conventions, it doesn't guarantee that they do it in the "standard" way.