How do I clear out a user object attribute in Acti

2020-03-01 16:55发布

问题:

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

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?

回答1:

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


回答2:

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