如何获得在Active Directory /更新“联系人”? [英] How to get/update 'Contacts' within Active Directory?

查看:167
本文介绍了如何获得在Active Directory /更新“联系人”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找到和更新Active Directory中的联系人?我建立一个样品C#.NET应用程序来实现这一任务。我想AP preciate任何code。

is there a way to find and update the contacts in Active Directory? I'm building a sample C# .NET application to achieve this task. I would appreciate any code.

推荐答案

当然,你可以在System.DirectoryServices中做到这一点。

Of course, you can do it in System.DirectoryServices.

我觉得你真正需要的是学习如何使用<一个href="http://msdn.microsoft.com/en-us/library/system.directoryservices.aspx">System.DirectoryServices.如果你没有一本好书呢,我建议这个

I think what you really need is to learn how to use System.DirectoryServices. If you don't have a good book yet, I recommend this one.

这并不难,真的。你只需要掌握两班,<一个href="http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.aspx">DirectoryEntry和<一href="http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.aspx">DirectorySearcher. <一href="http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.aspx">DirectoryEntry重新presenting LDAP服务器上的LDAP对象。假设你有足够的权限,你可以在任何LDAP对象的变化,包括使用的联系对象<一href="http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.aspx">DirectoryEntry.每个LDAP对象都有一些属性。两个你需要知道重要的属性是 objectCategory属性对象类。对于接触对象时,<​​code> objectCategory属性应对象类联系人。您可能还喜欢检查的targetAddress属性的联系对象,其中存储的电子邮件地址。有扩展的接触对象属性一堆交易所。你可能想通过一个检查每其中之一。要浏览LDAP服务器上的对象,你可以像使用 AD资源管理器或的ADSI修改

It's not that hard, really. You just need to master two classes, DirectoryEntry and DirectorySearcher. DirectoryEntry is representing a LDAP object on the LDAP server. Assuming you have sufficient permissions, you can make changes on any LDAP object, including the contact object using DirectoryEntry. Each LDAP object has a number of attributes. TWo important attributes you need to know are objectCategory and objectClass. For the contact object, the objectCategory should be person and objectClass should be contact. You may also like to check the "targetAddress" attribute on the contact object, which stores the email address. There are a bunch of Exchange extended attributes on contact object. You probably like to check each of them one by one. To browse the objects on LDAP server, you can use a tool like AD Explorer or ADSI Edit

要执行搜索,则需要提供四件事<一个href="http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.aspx">DirectorySearcher.

To do a search, you need to provider four things to DirectorySearcher.

  1. 搜索根目录
  2. 在LDAP搜索过滤器
  3. 搜索范围
  4. 在返回的属性

如果您的机器已经加入域,并且您登录时使用的域用户,这里是如何列出域中的所有联系人的样本。

If your machine is already joined to a domain and you are logging in as a domain user, here is a sample on how to list out all contacts in your domain.

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。

The first three lines are to help you to find the correct LDAP path to the root of your domain. It works only if you are logging in as a domain user. If you know the correct LDAP path of your domain, you can just feed it into DirectoryEntry directly.

我把所有四个参数为<一个href="http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.aspx">DirectorySearcher.当你逐渐熟悉目录服务的编程,你可以跳过其中一些​​和.NET将为您提供一个默认值。

I put all four parameters into DirectorySearcher. When you are getting familiar with Directory Services programming, you can skip some of them and .NET will provide a default value for you.

从<返回的结果href="http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.aspx">DiectorySearcher是<一个href="http://msdn.microsoft.com/en-us/library/system.directoryservices.searchresult.aspx">SearchResult.注意,<一href="http://msdn.microsoft.com/en-us/library/system.directoryservices.searchresult.aspx">SearchResult总是返回对象的集合,你即使的targetAddress 不是一个多值属性。这是因为一些LDAP对象上的属性可以是多值。

The result returned from DiectorySearcher is SearchResult. Note that SearchResult always return a collection of objects to you even though targetAddress is not a multivalue attribute. It's because some of the attributes on the LDAP object may be multi-value.

您可以从<一大家子的另一个重要信息href="http://msdn.microsoft.com/en-us/library/system.directoryservices.searchresult.aspx">SearchResult是路径。您可以创建一个<一个href="http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.aspx">DirectoryEntry以后使用此路径对象。要更新您的联系对象,你需要使用它的属性方法和的CommitChanges 方法。

Another important information you can get from SearchResult is the Path. You can create a DirectoryEntry object using this Path later. To update your contact object, you need to use its Properties method and CommitChanges method.

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

最后,你其实可以很容易地找到两个网上有很多教程<一href="http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.aspx">DirectorySearcher和<一href="http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.aspx">DirectoryEntry.试试Google吧。

Finally, you can actually easily find a lot of online tutorial on both DirectorySearcher and DirectoryEntry. Try google it.

这篇关于如何获得在Active Directory /更新“联系人”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆