Reputation: 682
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
Upvotes: 1
Views: 691
Reputation: 2887
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
Upvotes: 1