1

I am using Active Directory to authenticate a user logging into an ASP.NET web site on a corporate intranet. I am getting an error of "handle is invalid" on the following line of code:

Dim entry As DirectoryEntry = New DirectoryEntry(path, domainAndUsername, Password)

Here is my code I am using to authenticate. Dim entry As DirectoryEntry = New DirectoryEntry(path, domainAndUsername, Password)

    Try
        'Bind to the native AdsObject to force authentication.          

        Dim obj As Object = entry.NativeObject
        Dim search As DirectorySearcher = New DirectorySearcher(entry)

        search.Filter = "(SAMAccountName=" & Username & ")"
        search.PropertiesToLoad.Add("cn")
        Dim result As SearchResult = search.FindOne()

        If (result Is Nothing) Then
            Return False
        End If

        'Update the new path to the user in the directory.
        '_path = result.Path
        '_filterAttribute = CType(result.Properties("cn")(0), String)

    Catch ex As Exception
        Throw New Exception("Error authenticating user. " & ex.Message)
    End Try

How do I track down this exception? Visual Studio says it is a CryptographicException

Thanks

1

1 Answer 1

1

Assuming you are on .NET 3.5.... and thinking the code translates well to VB you could use built-in method.

public bool isValidUser(string password,string username,string domain)
        {
            var isValid = false;
            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain))
            {
                isValid = pc.ValidateCredentials(username, password);
            }
            return isValid;
        }

I know it does not answer question as such, but could avoid getting the exception in the first place

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.