How do you check if a computer account is disabled in Active Directory using C#/.NET
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
If you are using .NET 3.5, you can use the new System.DirectoryServices.AccountManagment namespace methods to much more easily access Active Directory. The UserPrincipal object has an Enabled property that gives you what you are looking for.
There is a good overview of these routines in the January 2008 MSDN Magazine. You can read the article online here: Managing Directory Security Principals in the .NET Framework 3.5
Try this:
The second bit of
userAccountControl
will be 1 if the account is disabled.If you are using samAcountName or any other Identity field.. it is way simpler to use UserPrincipal.FindByIdentity method. And use the hybrid approach to Leandro López and Deepti. both their approaches are very good.. but very narrowly focused. More details on this flag is available on MSDN
hey i got it finallyy :) here is my code hope it helps you
const int ADS_UF_ACCOUNTDISABLE = 0x00000002;
//for reference results.Properties["userAccountControl"][0].ToString().Equals("514");
You can easily decode the userAccountControl property by converting your result to an enum.
You can also use it to build an LDAP filter:
Also see Active Directory: LDAP Syntax Filters for examples of commonly useful Active Directory LDAP filters.
Here's the enum definition that you want:
Leandro López's answer is cool and works... the other option is we can do a LINQ for the userAccountControl with the values of disable and make those uses disabled
replie from userAccountControl are :
512 Enabled Account
514 Disabled Account
544 Enabled, Password Not Required
546 Disabled, Password Not Required
66048 Enabled, Password Doesn't Expire
66050 Disabled, Password Doesn't Expire
66080 Enabled, Password Doesn't Expire & Not Required
66082 Disabled, Password Doesn't Expire & Not Required
262656 Enabled, Smartcard Required
262658 Disabled, Smartcard Required
262688 Enabled, Smartcard Required, Password Not Required
262690 Disabled, Smartcard Required, Password Not Required
328192 Enabled, Smartcard Required, Password Doesn't Expire
328194 Disabled, Smartcard Required, Password Doesn't Expire
328224 Enabled, Smartcard Required, Password Doesn't Expire & Not Required
328226 Disabled, Smartcard Required, Password Doesn't Expire & Not Required