Page 1 of 1

check AD user/pass before opening a form

Posted: Tue Sep 26, 2017 4:15 am
by pls-sapien
Hi all but mostly jvierra :)
i have been reading many posts in this forum about how to add a user/pass mechanism to a form.
this is what i have but it only passes if i run it from a computer that a domain admin is logged on to localy

Code: Select all

function Test-Credentials ($cred)
{
	$username = $cred.username
	$password = $cred.GetNetworkCredential().password
	
	# Get current domain using logged-on user's credentials
	$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
	$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain, $UserName, $Password)
	
	if ($domain.name -eq $null)
	{
		[void][System.Windows.Forms.MessageBox]::Show('Authentication failed - please verify your username and password.', 'Fail')
		$form1.Close() #terminate the script.
		# return #use return if in an event block
	}
	else
	{
		[void][System.Windows.Forms.MessageBox]::Show("Successfully authenticated with domain $($domain.name)", 'Success')
		
	}
}
$form1_Shown={
	#TODO: Place custom script here
	$creds = Get-Credential
	Test-Credentials $Creds
}
when i take this app and run it from a regular domain user pc i keep getting authentication failed. if i run the app on a domain admin computer with the domain user credentials it works...
if i run the function from domain User pc in ISE or Powershell Studio in debug - it works...
not sure what am i missing...
thanks
Sean

Re: check AD user/pass before opening a form

Posted: Tue Sep 26, 2017 5:40 am
by pls-sapien
figured it out :D
the exe was saved on a network location, this for some reason messes up the validation. if i copy the file to local c: it will work

yay

Sean

Re: check AD user/pass before opening a form

Posted: Tue Sep 26, 2017 9:16 pm
by pls-sapien
in case someone else is having issues with get-credentials and validate them from a network share -

you must have full read access to the entire path of the share or the validation will fail.

for example: \\Server1\hiddenShare$\share1-NoReadAccedd\Share2-FullAccess\test-cred.exe

i did not have read permission to Share1. as soon as i fixed it - it works flawlessly.

best regards,

Sean