Share this page 

Authenticate a user (JNA/Windows) Tag(s): JNA


With a user/password and a domain, we authenticate a user.

import com.sun.jna.platform.win32.Advapi32;

import com.sun.jna.platform.win32.Kernel32;

import com.sun.jna.platform.win32.WinBase;

import com.sun.jna.platform.win32.WinNT;



    // https://github.com/twall/jna#readme

    //    you need 2 jars : jna-3.5.1.jar and platform-3.5.1.jar



    public static boolean authWindowsUser(String user, String domain, String password) {

        WinNT.HANDLEByReference handle = new WinNT.HANDLEByReference();

        boolean successful = Advapi32.INSTANCE.LogonUser

           (user, domain, password, WinBase.LOGON32_LOGON_NETWORK,

                   WinBase.LOGON32_PROVIDER_DEFAULT, handle);

        if (successful) {

           Advapi32.INSTANCE.RevertToSelf();

           Kernel32.INSTANCE.CloseHandle(handle.getValue());

        }

        return successful;

    }