How to get / update Contacts in Active Directory?

Is there a way to find and update contacts in Active Directory? I am creating a sample C # .NET application to achieve this. I would appreciate any code.

+3
source share
2 answers

Of course, you can do this in System.DirectoryServices.

I think you really need to learn how to use System.DirectoryServices . If you do not have a good book yet, I recommend this one .

, . , DirectoryEntry DirectorySearcher. DirectoryEntry LDAP LDAP. , , LDAP, , DirectoryEntry. LDAP . , , - objectCategory objectClass. objectCategory person, objectClass contact. "targetAddress" , . Exchange. , , . LDAP, , AD Explorer ADSI

, DirectorySearcher.

  • LDAP

, , .

DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
string domainContext = rootDSE.Properties["defaultNamingContext"].Value as string;
DirectoryEntry searchRoot = new DirectoryEntry("LDAP://" + domainContext);
using (DirectorySearcher searcher = new DirectorySearcher(
    searchRoot, 
    "(&(objectCategory=person)(objectClass=contact))", 
    new string[] {"targetAddress"}, 
    SearchScope.Subtree))
{
    foreach (SearchResult result in searcher.FindAll())
    {
        foreach (string addr in result.Properties["targetAddress"])
        {        
           Console.WriteLine(addr);
        }
        Console.WriteLine(result.Path);
    }
}

LDAP . , . LDAP , DirectoryEntry.

DirectorySearcher. , , .NET .

, DiectorySearcher, SearchResult, , SearchResult , targetAddress . , LDAP .

, SearchResult, Path. DirectoryEntry, . , Properties CommitChanges.

DirectoryEntry de = new DirectoryEntry(result.Path);
de.Properties["targetAddress"].Value = "SMTP:jane.doe@foo.bar";
de.CommitChanges();

, - DirectorySearcher, DirectoryEntry. Google.

+8

, Active Directory. , .

.Net 3.5 System.DirectoryServices.AccountManagement, AD , System.DirectoryServices.

( ) - :

string sUserName = "someusertoload";
string sDomain = "test.local";
string sDefaultOU = "OU=test,DC=test,DC=local";
string sServiceUser = "userwithrights";
string sServicePassword = "somepassword";
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU,ContextOptions.SimpleBind, sServiceUser, sServicePassword);
UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
oUserPrincipal.GivenName = "new givenname";
oUserPrincipal.Save();

.

.Net 2.0, "john" . , , , , .

DirectoryEntry root = new DirectoryEntry("LDAP://server/DC=test,DC=local");
DirectorySearcher searcher = new DirectorySearcher( root, "(&(objectCategory=person)(objectClass=user)(sAMAccountName=john))" );
SearchResult result = searcher.FindOne();
DirectoryEntry user = result.GetDirectoryEntry();
user.Properties["streetAddress"][0] = "My Street 12";
user.CommitChanges();
+5

Source: https://habr.com/ru/post/1793399/


All Articles