check AD user/pass before opening a form

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
Locked
User avatar
pls-sapien
Posts: 28
Joined: Tue Apr 05, 2016 9:03 pm

check AD user/pass before opening a form

Post by pls-sapien » Tue Sep 26, 2017 4:15 am

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

User avatar
pls-sapien
Posts: 28
Joined: Tue Apr 05, 2016 9:03 pm

Re: check AD user/pass before opening a form

Post by pls-sapien » Tue Sep 26, 2017 5:40 am

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

User avatar
pls-sapien
Posts: 28
Joined: Tue Apr 05, 2016 9:03 pm

Re: check AD user/pass before opening a form

Post by pls-sapien » Tue Sep 26, 2017 9:16 pm

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

Locked