首先你要知道自己用户的AD Path,例如LDAP://Domain.com/CN=user1,CN=Users,DC=domain,DC=com 然后使用System.DirectoryServices.DirectoryEntry就可以取得对应的AD对象了,其中的"memberof"属性就是用户所在的group的列表。 You can have a look with the sample in MSDN with following URL too: ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemdirectoryservicessearchresultclasstopic.htm Something like this: using System.DirectoryServices; ... string adPath = @"LDAP://Domain.com/CN=user1,CN=Users,DC=domain,DC=com"; DirectoryEntry user = new DirectoryEntry (adPath); foreach(object group in user.Properties["memberof"]) { Console.WriteLine(group.ToString()); }
System.DirectoryServices这个引用 点击菜单项目>点击添加引用>然后在.net里面选择就可以了.using System.Net;这个就不用说了吧?DirectoryEntry AD = new DirectoryEntry("WinNT://" + Dns.GetHostByAddress(IPAddress.Parse(textBox1.Text)).HostName + ",computer");Dns.GetHostByAddress(IPAddress.Parse(textBox1.Text)).HostName 这个是我通过输入IP地址来获取计算机名称的,你在实际使用过程中,可以直接写入计算机名称,比如:DirectoryEntry AD = new DirectoryEntry("WinNT://TempHost,computer");关于DirectoryEntry 的使用方法,你可以参考一下MSDN.如果你的测试不能通过.再留言吧.
想建立一个WebService
这个WebService需要实现2个功能(也就是说,添加2个方法)
1. AddGroup 添加Windows中的组(group)
2. AddUser 添加Windows中得用户(user)例如: 向Guest 组中加入一个用户 MyGuest1
{
DirectoryEntry AD = new DirectoryEntry("WinNT://" + Dns.GetHostByAddress(IPAddress.Parse(textBox1.Text)).HostName + ",computer");
DirectoryEntry xUser = AD.Children.Add ("GZB","user");
xUser.Invoke("SetPassword",new object[]{"gzb"});
xUser.Invoke("Put",new object[]{"description","test"});
xUser.CommitChanges(); DirectoryEntry GP = AD.Children.Find("Administrators","Group");
if ( GP!=null)
{
GP.Invoke( "ADD", new object[]{xUser.Path.ToString()} );
} MessageBox.Show("成功!");
}
catch( Exception ex )
{
MessageBox.Show ( ex.Message );
}
using System.Net;
然后通过楼上的方法
怎样获得呢?
然後在源代碼中加入using System.DirectoryServices
就可以應用了
有没有标准写法?
然后using System.DirectoryServices;
然后使用System.DirectoryServices.DirectoryEntry就可以取得对应的AD对象了,其中的"memberof"属性就是用户所在的group的列表。 You can have a look with the sample in MSDN with following URL too:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemdirectoryservicessearchresultclasstopic.htm Something like this: using System.DirectoryServices;
... string adPath = @"LDAP://Domain.com/CN=user1,CN=Users,DC=domain,DC=com";
DirectoryEntry user = new DirectoryEntry (adPath); foreach(object group in user.Properties["memberof"])
{
Console.WriteLine(group.ToString());
}
点击菜单项目>点击添加引用>然后在.net里面选择就可以了.using System.Net;这个就不用说了吧?DirectoryEntry AD = new DirectoryEntry("WinNT://" + Dns.GetHostByAddress(IPAddress.Parse(textBox1.Text)).HostName + ",computer");Dns.GetHostByAddress(IPAddress.Parse(textBox1.Text)).HostName 这个是我通过输入IP地址来获取计算机名称的,你在实际使用过程中,可以直接写入计算机名称,比如:DirectoryEntry AD = new DirectoryEntry("WinNT://TempHost,computer");关于DirectoryEntry 的使用方法,你可以参考一下MSDN.如果你的测试不能通过.再留言吧.