C# 如何清除 Active Directory 中的用户对象属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1197786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:39:16  来源:igfitidea点击:

How do I clear out a user object attribute in Active Directory?

c#active-directory

提问by Dscoduc

Suppose you have connected to Active Directory using the simple syntax:

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

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

Now, you find that you would like to see an attribute for that user. Let's try to display the mail attribute (which stands for email address):

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

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

How can I delete the mail attribute value, since setting it to an empty string will not throw an error?

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

采纳答案by Dscoduc

It turns out to be pretty simple, albeit not very commonly used...

事实证明它非常简单,尽管不是很常用......

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

回答by Jay

Not sure that you can delete it since user objects usually follow a company schema but maybe something like the following will work:

不确定您是否可以删除它,因为用户对象通常遵循公司架构,但可能如下所示:

userEntry.Properties["mail"] = null;

or maybe:

或者可能:

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

then:

然后:

userEntry.CommitChanges();