I want to get an account status in Active Directory (Active, Inactive,
Disabled) using Active Directory. Using the code that follows, I can
validate the account is active, but if the account is inactive or disabled,
it comes back with a false (Searcher is set to nothing). I can check to see
if an account is disabled by the following code:
Public Function IsAccountDisabled(ByVal strDomain As String, ByVal
strAccount As String) As Boolean
Dim objUser
objUser = GetObject("WinNT://" & strDomain & "/" & strAccount & ",user")
IsAccountDisabled = objUser.AccountDisabled
End Function
But this code is using COM and it sometimes cannot create the COM object. I
want to do all this using the System.DirectoryServices namespace but I am
having problems finding code to do this. Any help is much appreciated.
John
CODE:
Public Function ValidateActiveDirectoryLogin(ByVal Domain As String, ByVal
Username As String, ByVal Password As String) As Boolean
Dim Success As Boolean = False
Dim strDomain As String = "LDAP://" & Domain
Dim Entry As New DirectoryEntry(strDomain, Username, Password,
AuthenticationTypes.Secure)
Dim Searcher As New DirectorySearcher(Entry)
Searcher.SearchScope = SearchScope.OneLevel
Try
Dim Results As SearchResult = Searcher.FindOne
Success = Not (Results Is Nothing)
Catch ex As Exception
Success = False
End Try
Return Success
End Function