0

I'm trying to get all users from group in Active Directory. Everything is fine, but from 349 user to the end something doesn't working. It still saves properly "SamAccountName", but other properties are null (but in fact it has value). What is the solution for this problem ?

PrincipalContext ADDomain = new PrincipalContext(ContextType.Domain, daneAD.kontroler, AD.editDomena(daneAD.domena), daneAD.user, daneAD.haslo);
UserPrincipal u = new UserPrincipal(ADDomain);

GroupPrincipal group = GroupPrincipal.FindByIdentity(ADDomain, daneAD.grupa);

if (group != null)
{
    foreach (Principal p in group.GetMembers())
    {
        UserPrincipal result = p as UserPrincipal;

        if (result != null && result.DisplayName != null)
        {
            ADoutput user = new ADoutput();

            user.username = result.SamAccountName;
            user.firstname = result.GivenName;
            user.surname = result.Surname;
            user.mail = result.EmailAddress;
            user.isActive = result.Enabled;
            user.department = result.GetProperty("department");

            if (result.AccountExpirationDate.HasValue)
            {
               DateTime expiration = result.AccountExpirationDate.Value.ToLocalTime();

               user.lockdate = string.Format("TIMESTAMP '{0}-{1}-{2} {3}:{4}:{5}.{6}'", expiration.Year, expiration.Month, expiration.Day, expiration.Hour, expiration.Minute, expiration.Second, expiration.Millisecond);
            }
            else
            {
                user.lockdate = "null::timestamp";
            }

            // I save user there
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Side note: you could simplify your code by using: `user.lockdate = string.Format("TIMESTAMP '{0:yyyy-M-dd HH:mm:ss.fff}'", expiration);` – marc_s Oct 25 '14 at 10:31
  • 1
    Even simpler: lockdate = expiration.ToString("yyyy-M-dd HH:mm:ss.fff"); – WhoIsRich Oct 25 '14 at 11:27
  • As to the problem, hard to say without data access, but could it be there is a group within a group, or your running as a user that does not have access to some of the accounts? – WhoIsRich Oct 25 '14 at 11:34
  • Hmm, but it is possible to return only SamAccountName properly? I wonder why problem is only with last founded users. I also try GetMembers(true). – user3926523 Oct 25 '14 at 13:11
  • http://stackoverflow.com/questions/6722977/if-an-ou-contains-3000-users-how-to-use-directorysearcher-to-find-all-of-them maybe it is connected with some limits? But I don't have idea how to resolve it – user3926523 Oct 25 '14 at 13:43
  • 1
    Maybe try directing connecting to one of the affected users and see if it works. You could gather a list of user LDAP paths from the group first, then loop through them and bind to each user. – WhoIsRich Oct 25 '14 at 16:37

0 Answers0