locked
get attributes not in userPrinciple RRS feed

  • Question

  • User-1044426725 posted

    I am trying to get the data contained within the EmployeeNumber field in AD but cant work out how to do this as userPrinciple object doesn't contain this attribute.  How do I achieve this?

    Cheers

    Thursday, October 1, 2015 6:50 AM

All replies

  • User1508394307 posted

    Either try Principal Extensions or use DirectorySearcher, i.e.

    DirectorySearcher search = new DirectorySearcher(ldapConnection);
    search.Filter = string.Format("(&(ObjectClass=user)(userprincipalname=JOHN))",); 
    search.PropertiesToLoad.Add("employeeNumber");
    
    var result = search.FindOne();  
    if (result.Properties.Contains("employeeNumber"))
    {
        string employeeNumber = userEntry.Properties["EmployeeNumber"].Value.ToString();
    }


     

    Thursday, October 1, 2015 7:02 AM
  • User-1044426725 posted

    thank

    Does it matter which why I do it - are there pros and cons to each way?

    Thursday, October 1, 2015 7:48 AM
  • User1508394307 posted

    Using userPrinciple might be simpler compare to the other one, but "lacks" some functionality. On another hand, the UserPrincipal class is extensible, so you can "surface" more of the properties of the underlying DirectoryEntry object right on your user principal. 

    Thursday, October 1, 2015 8:17 AM
  • User-1044426725 posted

    Ok - the userPrinciple method in the link you provided was in fact what I was trying before I posted here! :)  But I couldn't get it to work.  This is my code:

     [DirectoryObjectClass("group")]
            [DirectoryRdnPrefix("CN")]
    
            public class GroupPrincipalsEx : GroupPrincipal
            {
                public GroupPrincipalsEx(PrincipalContext context) : base(context) { }
    
                public GroupPrincipalsEx(PrincipalContext context, string samAccountName) : base(context, samAccountName)
                {
                }
    
                [DirectoryProperty("EmployeeNumber")]
                public string EmployeeNumber
                {
                    get
                    {
                        if (ExtensionGet("EmployeeNumber").Length != 1)
                            return null;
    
                        return (string)ExtensionGet("EmployeeNumber")[0];
    
                    }
                    set { this.ExtensionSet("EmployeeNumber", value); }
                }
            }
    public static String GetCollarNoFromAD(string userName)
            {
                using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain))
                {
    
                    //UserPrincipal.FindByIdentity(domainContext, userName);
    
                    GroupPrincipalsEx oGroups = new GroupPrincipalsEx(domainContext, userName);
                    return oGroups.EmployeeNumber;
                }
    
            }

    This returns a null.  not sure which bit I need to edit and so am a bit stuck!  Any guidance greatly appreciated.

    cheers

    Thursday, October 1, 2015 8:57 AM
  • User1508394307 posted

    The example you took is for a GroupPrincipals, you however most likely need a UserPrincipal.

    So, in same way you need to extend UserPrincipal to get the required property. Here's the code I tested against my AD

    [DirectoryRdnPrefix("CN")]
    [DirectoryObjectClass("User")]
    public class UserPrincipalEx : UserPrincipal
    {
        // Implement the constructor using the base class constructor. 
        public UserPrincipalEx(PrincipalContext context) : base(context)
        { }
    
        // Implement the constructor with initialization parameters.    
        public UserPrincipalEx(PrincipalContext context,
                                string samAccountName,
                                string password,
                                bool enabled) : base(context, samAccountName, password, enabled)
        { }
    
        // Create the required property.    
        [DirectoryProperty("company")]
        public string EmployeeNumber
        {
            get
            {
                if (ExtensionGet("company").Length != 1)
                    return string.Empty;
    
                return (string)ExtensionGet("company")[0];
            }
            set { ExtensionSet("company", value); }
        }
    
        // Implement the overloaded search method FindByIdentity.
        public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue)
        {
            return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue);
        }
    
        // Implement the overloaded search method FindByIdentity. 
        public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
        {
            return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue);
        }
    }
    
    ...
    
    using (var context = new PrincipalContext(ContextType.Domain))
    {
        UserPrincipal u1 = UserPrincipal.FindByIdentity(context, "john");
        string x = u1.DisplayName;
                   
        UserPrincipalEx u2 = UserPrincipalEx.FindByIdentity(context, "john");
        string y = u2.EmployeeNumber;
    }

    In my test AD employee number was saved in the company-property, so you would need to change it to an appropriate value.

    Wednesday, October 7, 2015 7:34 AM