Following Referrals using .NET

As with PHP and Java, to follow referrals in .Net, you have to explicity tell .Net that you want the LDAP server to return the referral to you. In .Net, the default is for the .Net LDAP library to tell the LDAP server not to follow referrals. Below is an example of how to have .Net tell the LDAP server that referrals should be returned:

// Assume ldapID is a valid LdapDirectoryIdentifier object
ldapConn = new LdapConnection(ldapID);
ldapConn.SessionOptions.ProtocolVersion = 3;
ldapConn.SessionOptions.ReferralChasing = ReferralChasingOptions.All;

Thanks to Barry Waldman for providing this information for .Net.

Another more extended example is provided by Paul Hanson:

using (LdapConnection lConn = new LdapConnection(new LdapDirectoryIdentifier("ldap.berkeley.edu",389,false,false)))
{
lConn.AuthType = AuthType.Anonymous;
lConn.SessionOptions.ProtocolVersion = 3;
lConn.SessionOptions.ReferralChasing = ReferralChasingOptions.All;


SearchRequest _search = new SearchRequest();
_search.Filter = String.Format("(mail={0})", "pjhan@berkeley.edu");
_search.Scope = SearchScope.Subtree;


lConn.Bind();
var _sr = (SearchResponse)lConn.SendRequest(_search);
foreach (var item in _sr.Entries)
{
SearchResultEntry _sre = (SearchResultEntry)item;
Console.WriteLine(_sre.DistinguishedName);
}
Console.ReadKey();
}