【问题标题】:.Net code to set an Active Directory attribute to "not set".Net 代码将 Active Directory 属性设置为“未设置”
【发布时间】:2011-11-03 05:05:05
【问题描述】:

在 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。

【问题讨论】:

    标签: c# .net vb.net active-directory ldap


    【解决方案1】:

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

    【讨论】:

    • 轻微修正:属性是否可以没有值(或空值)取决于属性的语法。例如,IA5String 允许长度为零。
    • 那么这不是“无价值”而是“空值”。没有值真的是空的。
    猜你喜欢
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多