How I get Active Directory User Properties with System.DirectoryServices.AccountManagement Namespace?

前端 未结 2 722
广开言路
广开言路 2021-02-15 02:52

I want do get Active Directory Properties from a user and I want to use System.DirectoryServices.AccountManagement.

my code:

public static v         


        
2条回答
  •  梦谈多话
    2021-02-15 03:15

    You can transition into the DirectoryServices namespace to get any property you need.

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc);
    UserPrincipal u = UserPrincipal.FindByIdentity(ctx, user);
    
    string firstname = u.GivenName;
    string lastname = u.Surname;
    string email = u.EmailAddress;
    string telephone = u.VoiceTelephoneNumber;
    string company = String.Empty;
    
    ...//how I can get company and other properties?
    if (userPrincipal.GetUnderlyingObjectType() == typeof(DirectoryEntry))
    {
        // Transition to directory entry to get other properties
        using (var entry = (DirectoryEntry)userPrincipal.GetUnderlyingObject())
        {
            if (entry.Properties["company"] != null)
                company = entry.Properties["company"].Value.ToString();
        }
    }
    

提交回复
热议问题