Directory Services

Invoking ADSI Methods

If an ADSI interface supports the IDispatch interface, then you can use the DirectoryEntry method Invoke to access the methods on that interface. This also applies to any ADSI extensions that you may have added in the past. You do not need to include the ADSI library to use the Invoke method.

The following code example shows how to invoke the IADsUser method SetPassword to set a password.

[C#]
DirectoryEntry usr = new DirectoryEntry("LDAP://CN=John Smith, DC=Fabrikam,DC=COM");
usr.Invoke("SetPassword", new object[] {"secre@t!!!"});

The following code example shows how to invoke the IADsUser method ChangePassword to change a password.

[C#]
DirectoryEntry usr = new DirectoryEntry("LDAP://CN=John Smith, DC=Fabrikam,DC=COM");
usr.Invoke("ChangePassword", new object[] {"secre@t!!!","mynewsecre@t!#*"});

The following code example shows how to invoke the IADsGroup method Members to retrieve the members of a group.

[C#]
DirectoryEntry grpEntry = new DirectoryEntry("LDAP://CN=Enterprise Admins,CN=Users,DC=Fabrikam, DC=com");
object members = grpEntry.Invoke("Members",null);
foreach( object member in (IEnumerable) members)
{
	DirectoryEntry x = new DirectoryEntry(member);
	Console.WriteLine(x.Name);
}