How to clear attribute of user object in Active Directory?

Suppose you are connected to Active Directory using simple syntax:

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

Now you will find that you would like to see the attribute for this user. Let's try to display a mail attribute (denoting an email address):

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

How can I remove the value of the mail attribute since setting it to an empty string will not cause an error?

+8
c # active-directory
source share
2 answers

It turns out that it is quite simple, although not very often 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(); 
+18
source share

Not sure if you can remove it, since custom objects usually follow the corporate scheme, but maybe something like the following will work:

 userEntry.Properties["mail"] = null; 

or maybe:

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

then

 userEntry.CommitChanges(); 
0
source share

All Articles