如何在Active Directory中获取/更新“联系人”?

有没有办法在Active Directory中查找和更新联系人? 我正在构建一个示例C#.NET应用程序来完成此任务。 我会很感激任何代码。

当然,您可以在System.DirectoryServices中执行此操作。

我认为你真正需要的是学习如何使用System.DirectoryServices 。 如果你还没有好书,我推荐这本书。

真的,这并不难。 您只需要掌握两个类, DirectoryEntry和DirectorySearcher 。 DirectoryEntry表示LDAP服务器上的LDAP对象。 假设您具有足够的权限,则可以使用DirectoryEntry对任何LDAP对象(包括联系人对象)进行更改。 每个LDAP对象都有许多属性。 您需要知道的两个重要属性是objectCategoryobjectClass 。 对于contact对象, objectCategory应该是personobjectClass应该是contact 。 您还可以检查联系对象上的“targetAddress”属性,该属性存储电子邮件地址。 联系人对象上有许多Exchange扩展属性。 您可能希望逐一检查每一个。 要浏览LDAP服务器上的对象,可以使用AD Explorer或ADSI Edit等工具

要进行搜索,您需要向DirectorySearcher提供四件事。

  1. 搜索根
  2. LDAP搜索filter
  3. 搜索范围
  4. 返回属性

如果您的计算机已加入域并且您以域用户身份登录,则以下是有关如何列出域中所有联系人的示例。

 DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE"); string domainContext = rootDSE.Properties["defaultNamingContext"].Value as string; DirectoryEntry searchRoot = new DirectoryEntry("LDAP://" + domainContext); using (DirectorySearcher searcher = new DirectorySearcher( searchRoot, "(&(objectCategory=person)(objectClass=contact))", new string[] {"targetAddress"}, SearchScope.Subtree)) { foreach (SearchResult result in searcher.FindAll()) { foreach (string addr in result.Properties["targetAddress"]) { Console.WriteLine(addr); } Console.WriteLine(result.Path); } } 

前三行是帮助您找到域的根目录的正确LDAP路径。 仅当您以域用户身份登录时,它才有效。 如果您知道域的正确LDAP路径,则可以直接将其提供给DirectoryEntry。

我将所有四个参数放入DirectorySearcher 。 当您熟悉目录服务编程时,可以跳过其中一些,.NET将为您提供默认值。

从DiectorySearcher返回的结果是SearchResult 。 请注意,即使targetAddress不是多值属性, SearchResult也始终向您返回一组对象。 这是因为LDAP对象上的某些属性可能是多值的。

您可以从SearchResult获得的另一个重要信息是Path 。 您可以稍后使用此路径创建DirectoryEntry对象。 要更新联系对象,需要使用其Properties方法和CommitChanges方法。

 DirectoryEntry de = new DirectoryEntry(result.Path); de.Properties["targetAddress"].Value = "SMTP:jane.doe@foo.bar"; de.CommitChanges(); 

最后,您可以在DirectorySearcher和DirectoryEntry上轻松找到很多在线教程。 试试谷歌吧。

我认为您的意思是更新Active Directory中用户对象的属性。 是的,这是可能的。

使用.Net 3.5,我们获得了System.DirectoryServices.AccountManagement命名空间,与以前的System.DirectoryServices命名空间相比,它使得处理AD变得更加简单。

通常,要修改用户的属性(如果您有权保存),您可以执行以下操作:

 string sUserName = "someusertoload"; string sDomain = "test.local"; string sDefaultOU = "OU=test,DC=test,DC=local"; string sServiceUser = "userwithrights"; string sServicePassword = "somepassword"; PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU,ContextOptions.SimpleBind, sServiceUser, sServicePassword); UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName); oUserPrincipal.GivenName = "new givenname"; oUserPrincipal.Save(); 

你可以在这里找到一些辅助方法。

.Net 2.0的代码示例,用于检索用户名为“john”的用户并更新用户的街道地址。 如果运行用户的应用程序无权编辑内容,则可能必须向第一行添加凭据。

 DirectoryEntry root = new DirectoryEntry("LDAP://server/DC=test,DC=local"); DirectorySearcher searcher = new DirectorySearcher( root, "(&(objectCategory=person)(objectClass=user)(sAMAccountName=john))" ); SearchResult result = searcher.FindOne(); DirectoryEntry user = result.GetDirectoryEntry(); user.Properties["streetAddress"][0] = "My Street 12"; user.CommitChanges();