C# Source Code: How to check if a user's windows account is locked or disabled
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
How to check if a user's windows account is locked or disabled
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, November 03, 2005
Hits:
2567
Category:
General/Framework
Article:
The code below demonstates how to validate if a user's windows account has been locked or is disabled. using System.Runtime.InteropServices; #region API calls to check if windows account locked [DllImport("netapi32.dll", CharSet=CharSet.Unicode)] private extern static int NetUserGetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username,int level,out IntPtr bufptr); [DllImport("netapi32.dll", ExactSpelling=true)] private extern static int NetApiBufferFree( IntPtr pBuf ); [DllImport("netapi32.dll", CharSet=CharSet.Unicode, ExactSpelling=true)] private extern static int NetGetDCName( string servername, string domainname, out IntPtr bufptr ); [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] private struct USER_INFO_1 { public string usri1_name; public string usri1_password; public int usri1_password_age; public int usri1_priv; public string usri1_home_dir; public string comment; public int usri1_flags; public string usri1_script_path; } #endregion API calls to check if windows account locked ///
/// Checks if the user's windows account is locked or disabled. ///
///
The user name to test the status of. ///
Returns true if the account is locked or disabled, else returns false.
///
bool accountIsLocked = IsAccountLocked( Environment.UserName );
public static bool IsAccountLocked( string username ) { USER_INFO_1 userInfo; Type userInfoType = typeof(USER_INFO_1); const int NERR_Success = 0; const int UF_LOCKOUT = 16; const int UF_ACCOUNTDISABLE = 2; IntPtr bufptr; // Return level two information and additional attributes about the user account. // This level is valid only on Windows NT/Windows 2000 servers. // The bufptr parameter points to a USER_INFO_1 structure int retVal = NetUserGetInfo( GetDomainControllerName(), username, 1, out bufptr ); if ( retVal == NERR_Success ) { userInfo = (USER_INFO_1) Marshal.PtrToStructure( bufptr, userInfoType ); // Release memory retVal = NetApiBufferFree( bufptr ); bufptr = IntPtr.Zero; if ( (userInfo.usri1_flags & UF_ACCOUNTDISABLE) != 0 ) { // Account disabled return true; } else if ( (userInfo.usri1_flags & UF_LOCKOUT) != 0 ) { // Account locked return true; } // Account not locked return false; } else { // Failed (ignore error) } return false; } ///
/// Returns the name of the current users domain controller. ///
///
Returns the name of the current users domain controller.
public static string GetDomainControllerName() { const int NERR_Success = 0; IntPtr dcNamePtr; int retVal = NetGetDCName(null, null, out dcNamePtr); string dcName = null; if ( retVal == NERR_Success ) { dcName = Marshal.PtrToStringUni( dcNamePtr ); } // Clean up NetApiBufferFree( dcNamePtr ); dcNamePtr = IntPtr.Zero; return dcName; }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet