You can use the following code from here , which I translated from C # and changed a little according to your question (getter added):
Dim pwdNeverExpires = getPasswordNeverExpires("Tim") setPasswordNeverExpires("Tim", True) ' See http://msdn.microsoft.com/en-us/library/aa772300(VS.85).aspx <Flags()> _ Private Enum ADS_USER_FLAG_ENUM ADS_UF_SCRIPT = 1 ' 0x1 ADS_UF_ACCOUNTDISABLE = 2 ' 0x2 ADS_UF_HOMEDIR_REQUIRED = 8 ' 0x8 ADS_UF_LOCKOUT = 16 ' 0x10 ADS_UF_PASSWD_NOTREQD = 32 ' 0x20 ADS_UF_PASSWD_CANT_CHANGE = 64 ' 0x40 ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 128 ' 0x80 ADS_UF_TEMP_DUPLICATE_ACCOUNT = 256 ' 0x100 ADS_UF_NORMAL_ACCOUNT = 512 ' 0x200 ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 2048 ' 0x800 ADS_UF_WORKSTATION_TRUST_ACCOUNT = 4096 ' 0x1000 ADS_UF_SERVER_TRUST_ACCOUNT = 8192 ' 0x2000 ADS_UF_DONT_EXPIRE_PASSWD = 65536 ' 0x10000 ADS_UF_MNS_LOGON_ACCOUNT = 131072 ' 0x20000 ADS_UF_SMARTCARD_REQUIRED = 262144 ' 0x40000 ADS_UF_TRUSTED_FOR_DELEGATION = 524288 ' 0x80000 ADS_UF_NOT_DELEGATED = 1048576 ' 0x100000 ADS_UF_USE_DES_KEY_ONLY = 2097152 ' 0x200000 ADS_UF_DONT_REQUIRE_PREAUTH = 4194304 ' 0x400000 ADS_UF_PASSWORD_EXPIRED = 8388608 ' 0x800000 ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 16777216 ' 0x1000000 End Enum Protected Overridable Function getPasswordNeverExpires(ByVal userName As String) As Boolean Const userNameString As String = "userName" Const userFlagsString As String = "userFlags" Dim machineName As String = Environment.MachineName Dim userInThisComputerDirectoryEntry As DirectoryEntry = getUserInThisComputerDirectoryEntry(machineName, userName) If userInThisComputerDirectoryEntry Is Nothing Then Throw New ArgumentException("not found in " & machineName, userNameString) End If Dim userFlagsProperties As PropertyValueCollection = userInThisComputerDirectoryEntry.Properties(userFlagsString) Dim userFlags As ADS_USER_FLAG_ENUM = CType(userFlagsProperties.Value, ADS_USER_FLAG_ENUM) Return userFlags = (userFlags Or ADS_USER_FLAG_ENUM.ADS_UF_DONT_EXPIRE_PASSWD) End Function Protected Overridable Sub setPasswordNeverExpires(ByVal userName As String, ByVal passwordNeverExpires As Boolean) Const userNameString As String = "userName" Const userFlagsString As String = "userFlags" Dim machineName As String = Environment.MachineName Dim userInThisComputerDirectoryEntry As DirectoryEntry = getUserInThisComputerDirectoryEntry(machineName, userName) If userInThisComputerDirectoryEntry Is Nothing Then Throw New ArgumentException("not found in " & machineName, userNameString) End If Dim userFlagsProperties As PropertyValueCollection = userInThisComputerDirectoryEntry.Properties(userFlagsString) Dim userFlags As ADS_USER_FLAG_ENUM = CType(userFlagsProperties.Value, ADS_USER_FLAG_ENUM) Dim newUserFlags As ADS_USER_FLAG_ENUM = userFlags If passwordNeverExpires Then newUserFlags = newUserFlags Or ADS_USER_FLAG_ENUM.ADS_UF_DONT_EXPIRE_PASSWD Else newUserFlags = newUserFlags And (Not ADS_USER_FLAG_ENUM.ADS_UF_DONT_EXPIRE_PASSWD) End If userFlagsProperties.Value = newUserFlags userInThisComputerDirectoryEntry.CommitChanges() End Sub Protected Overridable Function getUserInThisComputerDirectoryEntry(ByVal machineName As String, ByVal userName As String) As DirectoryEntry Dim computerDirectoryEntry As DirectoryEntry = getComputerDirectoryEntry(machineName) Const userSchemaClassName As String = "user" Return computerDirectoryEntry.Children.Find(userName, userSchemaClassName) End Function Protected Overridable Function getComputerDirectoryEntry(ByVal machineName As String) As DirectoryEntry 'Initiate DirectoryEntry Class To Connect Through WINNT Protocol ' see: http:
You must add a link to System.DirectoryServices
. I tested it on Windows Server 2008 with the .NET Framework 4 (it should also work under 2.0) without Active Directory . But check it out yourself and feel free to extend it to get / set other properties or connect to other machines ( SomeDomain/OtherComputerName
instead of Environment.MachineName
).
source share