Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if user account is enabled or disabled

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?

like image 225
Bryan Avatar asked Jan 05 '10 11:01

Bryan


People also ask

How can I tell who is enabled a user account in Active Directory?

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.

How do I list disabled accounts in Active Directory PowerShell?

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”.

How do I check my Active Directory status?

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.


1 Answers

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); } 
like image 69
Dimi Takis Avatar answered Sep 19 '22 01:09

Dimi Takis