Click here to Skip to main content
14,093,107 members
Rate this:
 
Please Sign up or sign in to vote.
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:

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 ?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hi,

You can try following code.
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
   
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Like This:

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
   
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Quote:
Final Solution By Gurvinder Singh @ iGate


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



This always works for me
ContextOptions.Negotiate
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

you may need to use Try setting it with that
new UserPrincipal(pCtx, name, password, true);
to enable the account to prevent validation error.
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190518.1 | Last Updated 4 Jan 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100