how to lock/unlock an ad account

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lidia

    how to lock/unlock an ad account

    Anybody has any ideas?


  • clintonG

    #2
    Re: how to lock/unlock an ad account

    ASP.NET 2.0 Membership, Roles and Profiles provided support for locking and
    unlocking user accounts.

    "Lidia" <lli@omnitrans. comwrote in message
    news:%23i$FbxP4 IHA.4272@TK2MSF TNGP03.phx.gbl. ..
    Anybody has any ideas?
    >

    Comment

    • Lidia

      #3
      Re: how to lock/unlock an ad account

      Share my code to solve the problem mentioned above:



      Imports System.Director yServices



      'Lock:



      Public Function lockAcct(ByVal acct2Lock As String) As Boolean

      Dim blnreturn As Boolean = False

      Dim i As Integer = 0

      Dim path As String = gstrActiveDirec tory

      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.NativeObje ct



      'line2 section is to confirm the account has been locked

      Line2:

      Dim user2 As DirectoryEntry = adUser(acct2Loc k)

      If Convert.ToBoole an(user2.Invoke Get("IsAccountL ocked")) 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(ByVa l userid As String) As String

      Dim strReturn As String = ""

      Dim path As String = gstrActiveDirec tory

      Dim entry As DirectoryEntry = New DirectoryEntry( path)

      Try

      Dim searcher As New
      System.Director yServices.Direc torySearcher(en try)

      searcher.Filter =
      "(&(objectClass =user)(objectCa tegory=person)( sAMAccountName= " & userid &
      "))"

      searcher.Proper tiesToLoad.Add( "adspath")

      Dim Result As System.Director yServices.Searc hResult =
      searcher.FindOn e()

      If Not Result Is Nothing Then

      Dim user As New
      DirectoryServic es.DirectoryEnt ry(Result.Path)

      Try

      user.Properties ("LockOutTime") .Value = 0 'unlock account

      user.CommitChan ges()

      user.Close()

      strReturn = "successful "

      Catch err As UnauthorizedAcc essException

      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" <lli@omnitrans. comwrote in message
      news:%23i$FbxP4 IHA.4272@TK2MSF TNGP03.phx.gbl. ..
      Anybody has any ideas?
      >

      Comment

      Working...