public static string GetObjectDistinguishedName(objectClass objectCls, returnType returnValue, string objectName, string LdapDomain) { string distinguishedName = string.Empty; string connectionPrefix = "LDAP://" + LdapDomain; DirectoryEntry entry = new DirectoryEntry(connectionPrefix); DirectorySearcher mySearcher = new DirectorySearcher(entry); switch (objectCls) { case objectClass.user: mySearcher.Filter = "(&(objectClass=user)(|(cn=" + objectName + ")(sAMAccountName=" + objectName + ")))"; break; case objectClass.group: mySearcher.Filter = "(&(objectClass=group)(|(cn=" + objectName + ")(dn=" + objectName + ")))"; break; case objectClass.computer: mySearcher.Filter = "(&(objectClass=computer)(|(cn=" + objectName + ")(dn=" + objectName + ")))"; break; } SearchResult result = mySearcher.FindOne(); if (result == null) { throw new NullReferenceException ("unable to locate the distinguishedName for the object " + objectName + " in the " + LdapDomain + " domain"); } DirectoryEntry directoryObject = result.GetDirectoryEntry(); if (returnValue.Equals(returnType.distinguishedName)) { distinguishedName = "LDAP://" + directoryObject.Properties ["distinguishedName"].Value; } if (returnValue.Equals(returnType.ObjectGUID)) { distinguishedName = directoryObject.Guid.ToString(); } entry.Close(); entry.Dispose(); mySearcher.Dispose(); return(distinguishedName); }
internal static string GetObjectDistinguishedName(objectClass objectCls, returnType returnValue, string objectName, string LdapDomain) { string distinguishedName = string.Empty; string connectionPrefix = "LDAP://" + LdapDomain; DirectoryEntry entry = Utility.NewDirectoryEntry(connectionPrefix); DirectorySearcher mySearcher = new DirectorySearcher(entry); switch (objectCls) { case objectClass.user: mySearcher.Filter = "(&(objectClass=user)(|(cn=" + objectName + ")(sAMAccountName=" + objectName + ")))"; break; case objectClass.group: mySearcher.Filter = "(&(objectClass=group)(|(cn=" + objectName + ")(dn=" + objectName + ")))"; break; } SearchResult result = mySearcher.FindOne(); if (result == null) { throw new NullReferenceException("unable to locate the distinguishedName for the object " + objectName + " in the " + LdapDomain + " domain"); } DirectoryEntry directoryObject = result.GetDirectoryEntry(); if (returnValue.Equals(returnType.distinguishedName)) { distinguishedName = "LDAP://" + directoryObject.Properties["distinguishedName"].Value; } if (returnValue.Equals(returnType.ObjectGUID)) { distinguishedName = directoryObject.Guid.ToString(); } entry.Close(); entry.Dispose(); mySearcher.Dispose(); return distinguishedName; }
public string GetObjectDistinguishedName(objectClass objectCls, returnType returnValue, string objectName, string ldapDomain, LogFile log) { // LdapDomain = "DC=Fabrikam,DC=COM" string distinguishedName = string.Empty; string connectionPrefix = "LDAP://" + ldapDomain; try { DirectoryEntry entry = new DirectoryEntry(connectionPrefix); DirectorySearcher mySearcher = new DirectorySearcher(entry); switch (objectCls) { case objectClass.user: mySearcher.Filter = "(&(objectClass=user)(|(CN=" + objectName + ")(sAMAccountName=" + objectName + ")))"; break; case objectClass.group: mySearcher.Filter = "(&(objectClass=group)(|(CN=" + objectName + ")(dn=" + objectName + ")))"; break; case objectClass.computer: mySearcher.Filter = "(&(objectClass=computer)(|(CN=" + objectName + ")(dn=" + objectName + ")))"; break; case objectClass.organizationalunit: mySearcher.Filter = "(&(objectClass=organizationalunit)(distinguishedname=" + objectName + "))"; break; } SearchResult result = mySearcher.FindOne(); if (result == null) { //throw new NullReferenceException //("unable to locate the distinguishedName for the object " + //objectName + " in the " + LdapDomain + " domain"); return string.Empty; } DirectoryEntry directoryObject = result.GetDirectoryEntry(); if (returnValue.Equals(returnType.distinguishedName)) { distinguishedName = "LDAP://" + directoryObject.Properties ["distinguishedName"].Value; } if (returnValue.Equals(returnType.ObjectGUID)) { distinguishedName = directoryObject.Guid.ToString(); } entry.Close(); entry.Dispose(); mySearcher.Dispose(); } catch (Exception ex) { log.addTrn("searcher failed " + ldapDomain + " " + objectName + " Exception " + ex.Message.ToString() + "\n" + ex.StackTrace.ToString(), "Error"); } return distinguishedName; }