WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: JTaylor on March 31, 2026, 11:22:11 AM

Title: Use Windows Login For Authentication
Post by: JTaylor on March 31, 2026, 11:22:11 AM


LOGON32_LOGON_INTERACTIVE = 2
LOGON32_PROVIDER_DEFAULT = 0
hToken   = BinaryAlloc(8)   ; allocate buffer for HANDLE
username = Environment("USERNAME")
password = AskLine("Password","Enter Windows Password:","")

dll=DirWindows(1):"advapi32.dll"

ret = DllCall(dll, LONG:"LogonUserA", LPSTR:username, LPSTR:".", LPSTR:password, LONG:LOGON32_LOGON_INTERACTIVE, LONG:LOGON32_PROVIDER_DEFAULT, LPBINARY:hToken)
password = "CLEARPASSWORD"

If ret == 1 Then
    hTokenVal = BinaryPeek8(hToken, 0)
    DllCall("kernel32.dll", LONG:"CloseHandle", LONG:hTokenVal)
    Message("Login", "Password is VALID")
Else
    err = DllCall("kernel32.dll", LONG:"GetLastError")
    Message("Login", "Invalid password. Error: " : err)
EndIf
BinaryFree(hToken)

Title: Re: Use Windows Login For Authentication
Post by: kdmoyers on April 03, 2026, 04:33:44 AM
This is great Jim.

I had to fill in my Active Directory domain name where the "." is, and then it
checks against my Active Directory.

Nice stuff.
-Kirby
Title: Re: Use Windows Login For Authentication
Post by: JTaylor on April 03, 2026, 09:00:37 AM
Glad you liked it.   Yeah, there is more that can be done to verify by Group instead but I only needed this part so stopped here.

I have a situation where I wanted authentication but didn't want to have to deal with the entire system so got to wondering if I could do this.  Turns out, I could :-)


Jim