【问题标题】:How do I clear out a user object attribute in Active Directory?如何清除 Active Directory 中的用户对象属性?
【发布时间】:2009-07-29 02:31:06
【问题描述】:

假设您已使用简单语法连接到 Active Directory:

string adPath = "LDAP://server.domain.com/CN=John,CN=Users,dc=domain,dc=com";
DirectoryEntry userEntry = Settings.GetADEntry(adPath);

现在,您发现您希望查看该用户的属性。 让我们尝试显示邮件属性(代表电子邮件地址):

Console.WriteLine("User's mail attribute is " + userEntry.Properties["mail"]);

如何删除邮件属性值,因为将其设置为空字符串不会引发错误?

【问题讨论】:

    标签: c# active-directory


    【解决方案1】:

    其实很简单,虽然不是很常用...

    string adPath = "LDAP://server.domain.com/CN=John,CN=Users,dc=domain,dc=com";
    DirectoryEntry userEntry = Settings.GetADEntry(adPath);
    userentry.Properties["mail"].Clear();
    userentry.CommitChanges();
    

    【讨论】:

    • 完美的解决方案,无需使用带有方法名称的 Invoke。干杯!
    • 多年后,在关键场景中仍然有用。问候。
    • 谢谢!我想知道为什么 .Clear() 有效,但使用 userentry.Properties["mail"].Value = "" 不起作用。
    • 请注意,您不能清除具有空值的属性。它会抛出运行时错误。
    • @Pieter 你的意思是万一它抛出错误,你必须在里面放一个空字符串作为替代?
    【解决方案2】:

    不确定您是否可以删除它,因为用户对象通常遵循公司架构,但也许类似以下内容会起作用:

    userEntry.Properties["mail"] = null;
    

    或者也许:

    userEntry.Invoke("Put", "mail", null); 
    

    然后:

    userEntry.CommitChanges();
    

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      相关资源
      最近更新 更多