First add these external functions:
Function boolean LogonUser ( string lpszUsername, string lpszDomain,
string lpszPassword, ulong dwLogonType, ulong dwLogonProvider,
Ref long phToken ) Library "advapi32.dll" Alias For "LogonUserW"
Function boolean ImpersonateLoggedOnUser ( long hToken ) Library "advapi32.dll"
Function boolean RevertToSelf ( ) Library "advapi32.dll"
The following function can be used to impersonate any user that you know the password of. When done, call RevertToSelf.
public function boolean impersonate (string as_domain, string as_username, string as_password);// -----------------------------------------------------------------------------
// SCRIPT: Impersonate
//
// PURPOSE: This function allows you to change the security context to any valid user.
//
// To release the user security, call RevertToSelf.
//
// ARGUMENTS: as_domain - Domain Name (for local use single period)
// as_username - The username to connect as
// as_password - The password for the user
//
// RETURN: True=Success, False=Failed
//
// DATE PROG/ID DESCRIPTION OF CHANGE / REASON
// ---------- -------- -----------------------------------------------------
// 08/14/2020 RolandS Initial coding
// -----------------------------------------------------------------------------
Constant ULong LOGON32_LOGON_BATCH = 4
Constant ULong LOGON32_PROVIDER_DEFAULT = 0
Boolean lb_Result
Long ll_Token
lb_Result = LogonUser( as_username, &
as_domain, &
as_password, &
LOGON32_LOGON_BATCH, &
LOGON32_PROVIDER_DEFAULT, &
ll_Token )
If Not lb_Result Then Return False
lb_Result = ImpersonateLoggedOnUser(ll_Token)
If Not lb_Result Then Return False
Return True
end function