.Net代码将Active Directory属性设置为“未设置”

在Active Direcotry mmc管理单元中,您无法看到“未设置”的属性。 使用ADSIEDIT.MSC工具时,如果属性值为null,则会将其视为“未设置”。

如何在.Net代码中将属性设置为“未设置”?

以下是Powershell中的答案,但我需要使用一些.Net代码(VB.Net/C#)。 http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/d6d0bfa1-73da-41ea-a7f5-f622de9f7d1b/

ps msExchHideAddressLists是罪魁祸首属性,当此域中的True或False时,它会阻止用户信息从AD复制到Sharepoint。

在MSDN中,您可以找到:

在常用的支持LDAP的目录中,不存在没有值的属性。 通过更改,替换或追加操作将属性值设置为非空值时,如果该属性尚不存在,则创建该属性。 同样,如果将属性修改为没有值(或值),则删除整个属性。 有时您可能希望将属性设置为null。 虽然在支持LDAP的目录中不存在此概念,但您可以通过完全删除该属性并指定要清除该属性来实现此目的。

以下是使用System.DirectoryServices的示例:

 /* Connection to Active Directory */ DirectoryEntry deBase = new DirectoryEntry("LDAP://192.168.183.220:389/dc=societe,dc=local", "administrateur", "adm"); /* Directory Search */ DirectorySearcher dsLookForOUs = new DirectorySearcher(deBase); dsLookForOUs.Filter = "(objectCategory=organizationalUnit)"; dsLookForOUs.SearchScope = SearchScope.Subtree; dsLookForOUs.PropertiesToLoad.Add("cn"); dsLookForOUs.PropertiesToLoad.Add("ou"); dsLookForOUs.PropertiesToLoad.Add("telephoneNumber"); SearchResultCollection srcOUs = dsLookForOUs.FindAll(); foreach (SearchResult srOU in srcOUs) { Console.WriteLine("{0}", srOU.Path); DirectoryEntry de = srOU.GetDirectoryEntry(); if (de.Properties["TelephoneNumber"].Value!= null) { // Both solutions are working. Don't forget to commit //de.Properties["TelephoneNumber"].Clear(); de.Properties["TelephoneNumber"].Value=null; de.CommitChanges(); } }