调用 ADSI 方法

如果 ADSI 接口支持 IDispatch 接口,则可以使用 Invoke 方法访问该接口上的方法。这也适用于过去可能添加的任何 ADSI 扩展。不必包括 ADSI 库即可使用 Invoke 方法。

基本方法失败时,可能会引发 TargetInvocationException 异常。TargetInvocationException 对象的 InnerException 属性是包含有关所发生实际错误的信息的 COMException 对象。

下面的 C# 示例说明如何调用 IADsUser 接口的 SetPassword 方法以设置密码。有关 IADsUser 接口或 SetPassword 方法的详细信息,请参阅 MSDN Library(网址为 https://go.microsoft.com/fwlink/?LinkID=27252)中的“IADsUser”或“IADsUser::SetPassword”。

DirectoryEntry usr = new DirectoryEntry("LDAP://CN=John Smith, DC=Fabrikam,DC=COM");
usr.Invoke("SetPassword", new object[] {SecurelyStoredPassword});

下面的 C# 示例说明如何调用 IADsUser 接口的 ChangePassword 方法以更改密码。有关 IADsUser 接口或 ChangePassword 方法的详细信息,请参阅 MSDN Library(网址为 https://go.microsoft.com/fwlink/?LinkID=27252)中的“IADsUser”或“IADsUser::ChangePassword”。

DirectoryEntry usr = new DirectoryEntry("LDAP://CN=John Smith, DC=Fabrikam,DC=COM");
usr.Invoke("ChangePassword", new object[] {SecurelyStoredPassword, NewSecurelyStoredPassword});

下面的 C# 示例说明如何调用 IADsGroup 接口的 Members 方法以检索组的成员。有关 IADsGroup 接口或 Members 方法的详细信息,请参阅 MSDN Library(网址为 https://go.microsoft.com/fwlink/?LinkID=27252)中的“IADsGroup”或“IADsGroup::Members”。

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);
}

另请参见

参考

System.DirectoryServices
DirectoryEntry
TargetInvocationException
COMException
TargetInvocationException

概念

调用 ADSI

Send comments about this topic to Microsoft.

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。