There is an interesting email conversation going on over on the ADSI listgroup regarding the best way to determine if an account is locked out. One of the contributors has pointed out that Microsoft has updated the schema in ADAM and Windows 2003 to include a new constructed attribute called ‘msDS-User-Account-Control-Computed’. This attribute can accurately reflect the UF_LOCKOUT flag, unlike the standard ‘userAccountControl’ using the LDAP provider. The question comes out, which is the better method?
From my last post on this topic, I introduced a method of determining if an account was locked out when we only have the user’s DirectoryEntry. This method works on all platforms, including Windows 2000. It was correctly pointed out that this particular code also had some shortcomings. Key amongst these were that it used recursion to find the ‘lockoutDuration’ for the domain. I never liked that bit of code and I originally hesitated to use it. I did it anyway just so users could see where it was located. Now, I am going to revisit this again and perhaps show a better way of finding locked accounts that works on all platforms.
This is just sample code here, but it should give you an idea of what to do. I put in a lot of .WriteLine statements to hopefully make clear what is occurring. This dynamically determines the lockout duration policy and performs a simple search to determine which users are still locked out. This is done is only one call to the directory and is pretty efficient (add more indices to the filter and it gets better). If you were looking for only one user, you could obviously add that into the query and depending on whether or not you got a result back, you would know if they were locked out!
Here are the caveats:
Now, what about using the ‘msDS-User-Account-Control-Computed’ attribute? It works great when you have the DirectoryEntry and you know that you are using ADAM or Windows 2003. It is also decisive, i.e. if the bit is flipped you are locked out - no questions. It has the following limitations however:
So… which one should you use? That is completely up to you. Keep in mind the limitations of each and just pick one.
Standard Disclaimer: In the event this does not work for you… or utterly destroys your machine, I take no responsibility. This is sample code and not production ready. It has been through only limited testing, so test it yourself as well.
Comments [2] July 15, 2005 Trackback
This is the personal site of Ryan Dunn, co-author of the The .NET Developers Guide to Directory Services Programming.
Ryan currently works for Microsoft and is the Technical Evangelist for SQL Server Data Services (SSDS)
Buy the Book
Contact Ryan