I am throwing together a quick C# win forms app to help resolve a repetitive clerical job.
I have performed a search in AD for all user accounts and am adding them to a list view with check boxes.
I would like to default the listviewitems' default check state to depend upon the enabled/disabled state of the account.
string path = "LDAP://dc=example,dc=local"; DirectoryEntry directoryRoot = new DirectoryEntry(path); DirectorySearcher searcher = new DirectorySearcher(directoryRoot, "(&(objectClass=User)(objectCategory=Person))"); SearchResultCollection results = searcher.FindAll(); foreach (SearchResult result in results) { DirectoryEntry de = result.GetDirectoryEntry(); ListViewItem lvi = new ListViewItem( (string)de.Properties["SAMAccountName"][0]); // lvi.Checked = (bool) de.Properties["AccountEnabled"] lvwUsers.Items.Add(lvi); }
I'm struggling to find the right attribute to parse to get the state of the account from the DirectoryEntry object. I've searched for AD User attributes, but not found anything useful.
Can anyone offer any pointers?
Run gpedit. msc → Create a new GPO → Edit it : Go to "Computer Configuration" → Policies → Windows Settings → Security Settings → Local Policies → Audit Policy: Audit account management → Define → Success.
Run Netwrix Auditor → Navigate to “Reports” → Expand the “Active Directory” section → Go to “Active Directory – State-in-Time” → Select “User Accounts” → Click “View” → Set the “Status” parameter to “Disabled” → Click “View Report”.
Check AD account lockout status In ADUC, navigate to the properties of the user, then the Account tab. You will see the following message if an account is locked out: Unlock account. This account is currently locked out on this Active Directory Domain Controller.
this code here should work...
private bool IsActive(DirectoryEntry de) { if (de.NativeGuid == null) return false; int flags = (int)de.Properties["userAccountControl"].Value; return !Convert.ToBoolean(flags & 0x0002); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With