nauman_ahmed
asked on
Query an Active Directory Organizational Unit
hi,
How can I query the active directory organization unit for users.
Thanks for the help, Nauman.
How can I query the active directory organization unit for users.
Thanks for the help, Nauman.
ASKER
Thanks for the reply michael.
I need to grab the list of users from the organizational unit.
-Nauman.
I need to grab the list of users from the organizational unit.
-Nauman.
ASKER
I am trying to connect to the domain controller using IP address. However cannot make LDAP://192.168.0.10,OU=ADM IN_USERS work. LDAP://192.168.0.10 is working fine.
-Nauman.
-Nauman.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks raterus. LDAP://192.168.0.10/OU=ADM IN_USERS didnt work, but your second option worked. I have another question. This piece of code is giving me The Network path could not be found exception. I am trying to reset user password but still dont have any luck. Its in c# but I think its easy to understand. If you have any vb.net code, please paste it. I can translate that into C#:
DirectoryEntry entry = new DirectoryEntry("LDAP://192 .168.0.10" ,"dmain\\a dministrat or","admin ",Authenti cationType s.Secure);
object native = entry.NativeObject;
Console.WriteLine("User authenticated.");
DirectoryEntry ou = entry.Children.Find("OU=AD MIN_USERS" );
DirectorySearcher srch = new DirectorySearcher(ou);
srch.Filter = "samaccountname=ap_admin";
SearchResult search = srch.FindOne();
DirectoryEntry user = search.GetDirectoryEntry() ;
user.Properties["descripti on"].Value = "abcasaasasasasa";
object[] obj = {"aaa"};
user.Invoke("SetPassword", obj);
user.CommitChanges();
Console.WriteLine(search.P roperties. Count);
Thanks, Nauman.
DirectoryEntry entry = new DirectoryEntry("LDAP://192
object native = entry.NativeObject;
Console.WriteLine("User authenticated.");
DirectoryEntry ou = entry.Children.Find("OU=AD
DirectorySearcher srch = new DirectorySearcher(ou);
srch.Filter = "samaccountname=ap_admin";
SearchResult search = srch.FindOne();
DirectoryEntry user = search.GetDirectoryEntry()
user.Properties["descripti
object[] obj = {"aaa"};
user.Invoke("SetPassword",
user.CommitChanges();
Console.WriteLine(search.P
Thanks, Nauman.
ASKER
Actually this was an authentication problem and was b/c of using AuthenticationTypes.Secure seting. AuthenticationTypes.Signin g or AuthenticationTypes.Sealin g worked in changing the password. Thanks for the hielp.
-Nauman.
-Nauman.
You can use DirectorySearcher to query users in a organizational unit, it is much faster than other way. It returns SearchResultCollection which contains user object collection. And at each user object you can retrieve information like login name, email and many other but not password because it is write-only attribute. Password in AD is a complicated thing and there's no way to do so that I'm aware of. But you can always create a custom attribute to store user password and retrieve it back at any time.
Dim baseDN As String = "LDAP://oneCity/OU=Users,D C=oneCity, DC=com"
Dim filter As String = "(&(objectClass=user)(obje ctCategory =person))"
Dim searcher As DirectorySearcher
Try
searcher = New DirectorySearcher
searcher.SearchRoot = New DirectoryEntry(baseDN)
searcher.SearchScope = SearchScope.Subtree
searcher.Filter = filter
Dim results As SearchResultCollection = searcher.FindAll()
For Each result As SearchResult In results
'each SearchResult object here is a AD user class
Dim loginName As String = CType(result.Properties("c n")(0), String)
Dim email As String = CType(result.Properties("m ail")(0), String)
Next
Catch ex As Exception
Finally
If Not IsNothing(searcher) Then
searcher.Dispose()
End If
End Try
HTH
Dim baseDN As String = "LDAP://oneCity/OU=Users,D
Dim filter As String = "(&(objectClass=user)(obje
Dim searcher As DirectorySearcher
Try
searcher = New DirectorySearcher
searcher.SearchRoot = New DirectoryEntry(baseDN)
searcher.SearchScope = SearchScope.Subtree
searcher.Filter = filter
Dim results As SearchResultCollection = searcher.FindAll()
For Each result As SearchResult In results
'each SearchResult object here is a AD user class
Dim loginName As String = CType(result.Properties("c
Dim email As String = CType(result.Properties("m
Next
Catch ex As Exception
Finally
If Not IsNothing(searcher) Then
searcher.Dispose()
End If
End Try
HTH
ASKER
Thanks for the resposne ihenry. But here I am with another problem :( Things work fine on console application but and once in asp.net but after that asp.net page stopped working and it gave the following exception:
System.Runtime.InteropServ ices.COMEx ception: One or more input parameters are invalid
System.RuntimeType.InvokeD ispMethod( String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) +0
System.RuntimeType.InvokeM ember(Stri ng name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters) +473
System.Type.InvokeMember(S tring name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args) +29
System.DirectoryServices.D irectoryEn try.Invoke (String methodName, Object[] args) +106
The line that giving exception is user.Invoke("SetPassword", obj); The other update work fine.
I can open a separate question if you would like.
Thanks for the help, Nauman.
System.Runtime.InteropServ
System.RuntimeType.InvokeD
System.RuntimeType.InvokeM
System.Type.InvokeMember(S
System.DirectoryServices.D
The line that giving exception is user.Invoke("SetPassword",
I can open a separate question if you would like.
Thanks for the help, Nauman.
what's the obj variable? SetPassword should be used like the following:
user.Invoke("SetPassword", new object[]{"" + NewPassword + ""});
user.Invoke("SetPassword",
Change password using SetPassword is a bit tricky. How are you invoking the SetPassword method? what user are you binding to active directory with? and are you binding using secure channel?
You'd start out with a DirectorySearcher, that is instantiated something like this.
Dim oRootDSE As DirectoryEntry = New DirectoryEntry("LDAP://roo
sDomain = CStr(oRootDSE.Properties("
Dim entry as DirectoryEntry = New DirectoryEntry("LDAP://" & sDomain & ",OU=SOME_OU")
Dim searcher As DirectorySearcher = New DirectorySearcher(entry)
--Michael