Retrieving custom Active Directory properties of users

后端 未结 2 1005
孤城傲影
孤城傲影 2020-12-21 19:25

I\'ve been trying to retrieve two custom properties that are set on our Active Directory users, but it seems like I keep getting a constant list of Properties (tested over 2

相关标签:
2条回答
  • 2020-12-21 19:44

    Though not a direct answer to your question, following is what we use:

            public static string GetProperty(string adUserId, string domain, string lDAPLoginId, string lDAPPassword, string propertyName)
            {
                PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain, lDAPLoginId, lDAPPassword);
                UserPrincipal up = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, adUserId);
    
                string result = "";
    
                if (up != null)
                {
                    result = PrincipalGetProperty(up, propertyName);
                }
    
                return result;
            }
    
    0 讨论(0)
  • 2020-12-21 19:56

    Are you sure your properties are set? if they are not set, they are not gonna be listed as properties.

    If you're looking for specific properties, I'd recommend you to use DirectorySearcher

    The following example is getting the company property of a given user. Note that you verify if the property exists first, then you extract it.

    DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://DOM");
    
    //Create a searcher on your DirectoryEntry
    DirectorySearcher adSearch= new DirectorySearcher(directoryEntry);
    adSearch.SearchScope = SearchScope.Subtree;    //Look into all subtree during the search
    adSearch.Filter = "(&(ObjectClass=user)(sAMAccountName="+ username +"))";    //Filter information, here i'm looking at a user with given username
    SearchResult sResul = adSearch.FindOne();       //username is unique, so I want to find only one
    
    if (sResult.Properties.Contains("company"))     //Let's say I want the company name (any property here)
    {
        string companyName = sResult.Properties["company"][0].ToString();    //Get the property info
    }
    
    0 讨论(0)
提交回复
热议问题