我有以下代码(C#):
(从调整了: http://www.eggheadcafe.com/conversation.aspx?messageid=31766061&threadid=31766050 )
DirectorySearcher dseSearcher = new DirectorySearcher();
string rootDSE = dseSearcher.SearchRoot.Path;
DirectoryEntry rootDE = new DirectoryEntry(rootDSE);
string userDSE = rootDSE.Insert(7, "OU=Users,");
DirectoryEntry userDE = new DirectoryEntry(userDSE);
该rootDSE
是正确创建,但是,用户userDSE
不可用,并抛出“有在服务器上没有这样的对象”异常,如果我尝试使用它。
在LDAP字符串如下:
根:LDAP:// DC =公司,DC =本地
用户:LDAP:// OU =用户,DC =公司,DC =本地
我在Vista上运行的管理,但需要这在XP(管理员)正常工作。
我是新来的LDAP和目录管理,所以我在黑暗中跌跌撞撞这里周围。 有什么想法吗? 此外 - 所有文章链接太多,可以给我一些见解,所有的工作将如何理解。
我想尝试作为测试的第一件事是,当你创建了这样一个目录条目进行硬编码所需的路径:
DirectoryEntry de = new DirectoryEntry("LDAP://OU=Users,DC=company,DC=local");
这会告诉你相当快,如果这是在Active Directory的实际路径。 我不知道自己的广告是什么样子,所以我不能告诉你,如果这是一个有效的路径或没有。 在您的Active Directory用户和计算机MMC插件,如果这条道路是正确的,那么你应该有你的根域,和根称为用户下OU文件夹。
路径在AD向后产生,因此,如果您的用户文件夹是由另一OU关闭根比这将是
DirectoryEntry de = new DirectoryEntry("LDAP://OU=Users,OU=<first OU folder>,DC=company,DC=local");
所以,你的AD架构将如下所示:
Root
|
--><first OU folder>
|
-->Users
如何在.NET管理Active Directory一个伟大的文章:
Howto:如何通过C#做的(几乎)一切在Active Directory
您可能还需要来研究的System.DirectoryServices,System.DirectoryServices.ActiveDirectory,并在.NET 3.5 Framework提供的System.DirectoryServices.AccountManagement命名空间。 我相信的System.DirectoryServices和ActiveDirctory命名空间是可用的。Net 1.1盯着,并AccountManagement以.Net 3.5推出。
微软文档-很多关于如何使用命名空间良好的联系
附录:
要真正找到广告的用户,您将要做到以下几点:
DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://DC=company,DC=local";
de.AuthenticationType = AuthenticationTypes.Secure;
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = "(&(objectClass=user) (cn=" + username + "))";
SearchResult result = deSearch.FindOne();
if (result != null)
{
DirectoryEntry deUser = new DirectoryEntry(result.Path);
... do what ever you need to the deUser
deUser.Close();
}
这可能看起来很可笑和愚蠢的,但默认树设置在Active Directory不是OU =用户 ,DC =域,DC = COM而是CN =用户 ,DC =域,DC = COM(注CN =不是OU =供用户使用。
看来愚蠢的,因为在广告中的容器对象(CN的对象类),不能是组策略的一个收件人,但对于原因,我不明白,这是默认。 (其实我也明白了,那是因为遏制了CN更类似于NT域比OU)
获取几乎每个人我见面,他们第一次尝试LDAP绑定/ AUTH到AD。
由于正确地提到geoffc,Active Directory中的“用户”的域下是容器对象,而不是组织单元对象。 这导致这就是为什么你会得到错误信息完全不同的LDAP路径。
试试下面的代码和后能否解决您的问题:
// Replace the "company" and "com" with actual domain values...
DirectoryEntry de = new DirectoryEntry("LDAP://CN=Users,DC=company,DC=com");
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
// Set your other search params here