2011-11-03 92 views
4

在Active Direcotry mmc管理单元中,您看不到属于“未设置”的属性。当您使用ADSIEDIT.MSC工具时,如果属性值为空,您会将它们视为“未设置”。.Net代码将Active Directory属性设置为“未设置”

如何在.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的用户信息。

回答

3

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(); 
    } 
} 
+0

轻微修正:属性是否可以具有任何值(或空值)取决于属性的语法。例如,IA5String被允许为零长度。 –

+0

然后这不被视为'没有价值',而是'空值'。没有值真的是空的。 – JPBlanc