1

I am constructing a URI for an LDAP connection. I know Microsoft does support serverless binding using ldap:///cn=... notation, and that URI will work with .NET, but UriBuilder will not produce a correct URI if port is specified.

For example:

var ub = new UriBuilder();
ub.Scheme = "ldap";
ub.Host = ""
// ---> ub.Uri.ToString() : "ldap:///"
ub.Port = 389
// ---> ub.Uri.ToString() : "ldap:///"
// ok, lets put non-standard port
ub.Port = 3899
// ---> ub.Uri.ToString() : "ldap:///"
ub.UserName = "user"
// ---> ub.Uri.ToString() : 
//      'ub.Uri' threw an exception of type 'System.UriFormatException'

EDIT: Notice: if Host is "", assigning value to Port has no effect, and assigning anything to UserName has an effect of an exception.

Is that a .NET problem, or is there not really a proper standard URI for serverless binding at all?

galets
  • 17,802
  • 19
  • 72
  • 101

1 Answers1

1

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.

Community
  • 1
  • 1
Athari
  • 33,702
  • 16
  • 105
  • 146
  • idk, that really was the essense of my question :) – galets May 22 '15 at 17:55
  • @galets Well, what does LDAP expect? Only scheme and path? Or does it support specifying other parts if host isn't present? – Athari May 22 '15 at 17:57
  • ldap in Active Directory can be used without host name, it will use current domain controller. It could also be used with alternative port (to access GC instead of DC), or with alternative credentials - all of that without host name. – galets May 22 '15 at 18:03
  • @galets What does URI in these cases look like? – Athari May 22 '15 at 18:05
  • As I already said, I have no idea, that essentially is what I am asking. There are monikers for some standard naming. For example, localhost for - well - localhost. Sometimes dot (".") is used in lieu of default host name (which does not work in UriBuilder). I was hoping there is maybe some standard host name which stands for "not real host name, use serverless binding here". – galets May 22 '15 at 18:09