Share my code to solve the problem mentioned above:
Imports System.DirectoryServices
'Lock:
Public Function lockAcct(ByVal acct2Lock As String) As Boolean
Dim blnreturn As Boolean = False
Dim i As Integer = 0
Dim path As String = gstrActiveDirectory
Dim user As DirectoryEntry = Nothing
Try
'since it's hard to use code to lock an account, use 4 attempts
to login and let AD handle it
'line1 section is to try to login using 1 as password
Line1:
i = i + 1
user = New DirectoryEntry(path, acct2Lock, 1)
Dim nativeObject As Object = user.NativeObject
'line2 section is to confirm the account has been locked
Line2:
Dim user2 As DirectoryEntry = adUser(acct2Lock)
If Convert.ToBoolean(user2.InvokeGet("IsAccountLocked ")) Then
blnreturn = True
End If
Catch ex As Exception
If i < 4 Or i = 4 Then
GoTo Line1
End If
GoTo Line2
End Try
Return blnreturn
End Function
'Unlock:
Public Function unlockAcct(ByVal userid As String) As String
Dim strReturn As String = ""
Dim path As String = gstrActiveDirectory
Dim entry As DirectoryEntry = New DirectoryEntry(path)
Try
Dim searcher As New
System.DirectoryServices.DirectorySearcher(entry)
searcher.Filter =
"(&(objectClass=user)(objectCategory=person)(sAMAc countName=" & userid &
"))"
searcher.PropertiesToLoad.Add("adspath")
Dim Result As System.DirectoryServices.SearchResult =
searcher.FindOne()
If Not Result Is Nothing Then
Dim user As New
DirectoryServices.DirectoryEntry(Result.Path)
Try
user.Properties("LockOutTime").Value = 0 'unlock account
user.CommitChanges()
user.Close()
strReturn = "successful"
Catch err As UnauthorizedAccessException
strReturn = err.Message
End Try
Else
strReturn = "No such user in the active directory"
End If
Catch ex As Exception
'do something here
End Try
Return strReturn
End Function
"Lidia" <ll*@omnitrans.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Anybody has any ideas?