ValidateCredentials throws incorrect result

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
lokesh.gunjugnur
Posts: 2
Last visit: Fri Jun 12, 2020 8:06 am

ValidateCredentials throws incorrect result

Post by lokesh.gunjugnur »

Ok, This is a weird behavior I see between Powershell ISE and Powershell Studio. The code below gives totally opposite results when evaluated.

The Powershell ISE throws the value as true as it should be.
Powershell Studio throws the value as False which is incorrect. Debugging shows its receiving the correct values to evaluate. Anyone encountered this issue or can you emulate the same. I am working with Powershell Studio 2017 and Windows powershell version 5.0. Any insight is much appreciated.

$ACCOUNT = 'UserName'
$domain = 'DomainName'
$AccountFQDN = $domain + '\' + $ACCOUNT
$Password = "******"

#In the event, user verifies verifies the password for account

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($ct, $domain)
$value = $pc.ValidateCredentials($ACCOUNT , $Password,[System.DirectoryServices.AccountManagement.ContextOptions]::Negotiate).ToString()


If ($Value -eq $true)
{
Write-Host ("Authentication Successful? - $value `n ")
}
ElseIf ($Value -eq $false)
{
Write-Host ("Authentication UnSuccessful? - $value `n ")
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ValidateCredentials throws incorrect result

Post by jvierra »

The following is a bad construct and may easily cause many issues depending on where it is used.

Code: Select all

If ($Value -eq $true)
{
Write-Host ("Authentication Successful? - $value `n ") 
}
ElseIf ($Value -eq $false)
{
Write-Host ("Authentication UnSuccessful? - $value `n ") 
}
The correct method for testing a Boolean is as follows:

Code: Select all

If ($Value){
    Write-Host ("Authentication Successful? - $value `n ") 
}else{
    Write-Host ("Authentication UnSuccessful? - $value `n ") 
}
A Boolean does not need to be tested against boolean and may fail under many circumstances.

You are also converting the options to a string. This is not necessary and a string "false" will always be true.

$value = $pc.ValidateCredentials($ACCOUNT , $Password,[System.DirectoryServices.AccountManagement.ContextOptions]::Negotiate).ToString()

The correct sybtax and construct for this is as follows:

Code: Select all

Add-Type -AssemblyName System.DirectoryServices.AccountManagement 
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($ct, $domain)
if($pc.ValidateCredentials($ACCOUNT , $Password,'Negotiate')){
    Write-Host 'Authentication Successful!'
}else{
    Write-Host 'Authentication UnSuccessful!' 
} 
User avatar
lokesh.gunjugnur
Posts: 2
Last visit: Fri Jun 12, 2020 8:06 am

Re: ValidateCredentials throws incorrect result

Post by lokesh.gunjugnur »

Thanks for the clarification. Yes, its much cleaner and precise to evaluate the Boolean in the manner you explained. Its all good at my end now. I appreciate your time and the quick response.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ValidateCredentials throws incorrect result

Post by jvierra »

You are welcome. Try to break the habit of testing Booleans against a Boolean. It will get you in many weird ways.
This topic is 6 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked