Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to use this method on an application, so that a user would log in by entering their Windows network user id and password:

VB
Imports System.DirectoryServices.AccountManagement

    Public Function IsValid(ByVal UserId As String, ByVal Password As String) As Boolean
        Dim Result As Boolean = False

        Try
            Dim PC As New PrincipalContext(ContextType.Domain, "MyDomain")
            Result = PC.ValidateCredentials(UserId, Password)
        Catch ex As Exception
            Throw ex
        End Try

        Return Result
    End Function


Every piece of documentation I have found shows this to be correct. I have verified that PC is being created properly and is pointing to the correct network domain. I have verified that the userid and password are both correct, and that the user exists in the requested domain. And yet, the call to PC.ValidateCredentials always returns false.

I have a another method, which goes through ActiveDirectory, but it involves a lot of overhead and takes about 10 seconds to validate. That one returns true with the same domain, userid and password.

I assume I'm doing something wrong. Any suggestions?
Posted
Comments
ledtech3 18-May-12 14:03pm    
try removing the "= False" from "Dim Result As Boolean = False" and see what happens.
Improved:
By declareing it to false outside of your try catch block, not matter what happens inside it will always revert to False once leaving the try /catch.
Gregory Gadow 18-May-12 15:37pm    
First, if I give it no explicit initial value, VB.Net will assign it a default value of False. Second, any change to a variable will persist within its scope. The variable is declared at the function level, so any change within the function scope will persist within the function scope, even if that change is made within a narrower scope like a Try...Catch block.
ledtech3 18-May-12 15:42pm    
Have you tried the code without it set to False ?

Hi,

You can try following code.
C#
Using pc = New PrincipalContext(ContextType.Domain, "domain.lan", username, password)
	If pc.ValidateCredentials(username, password) Then
		Try
			Using searcher = New PrincipalSearcher(New UserPrincipal(pc))
				searcher.QueryFilter.SamAccountName = username
				Dim u As Principal = searcher.FindOne()
			End Using
		Catch generatedExceptionName As Exception
			Return "no rights to work on ad"
		End Try
	Else
		Return "user cannot login"
	End If
End Using

Hope this will help you.
thanks
 
Share this answer
 
v2
Like This:

VB
Imports System.DirectoryServices.AccountManagement

Public Function IsValid(ByVal UserId As String, ByVal Password As String) As Boolean
    Dim Result As Boolean

    Try
        Result = False
        Dim PC As New PrincipalContext(ContextType.Domain, "MyDomain")
        Result = PC.ValidateCredentials(UserId, Password)
    Catch ex As Exception
        Throw ex
    End Try

    Return Result
End Function
 
Share this answer
 
Comments
Gregory Gadow 18-May-12 15:38pm    
Result is defined at the function level. Without an explicit assign at declaration, VB.Net automatically assigns it a value of False, making the assignment within the Try block redundant.
ledtech3 18-May-12 15:53pm    
In the code above then,Remove the "Result = False" then and add a message box just before the catch statement and see if it is returning anything true when "Return Result" returns False, if it is then it is a problem with the code that calls the function if not then perhaps the credintals are invalid. Thats just the way I catch some of the problems.Or step thru the code in the debugger and see what the value is inside of the try/catch block.
Quote:
Final Solution By Gurvinder Singh @ iGate


C#
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, serverName))
              {
                  return pc.ValidateCredentials(userName, password, ContextOptions.Negotiate);
              }



This always works for me
ContextOptions.Negotiate
 
Share this answer
 
you may need to use Try setting it with that
new UserPrincipal(pCtx, name, password, true);
to enable the account to prevent validation error.
 
Share this answer
 
Comments
CHill60 23-Sep-13 15:33pm    
Unclear (and the question is over a year old)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900