Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Logonuser impersonate user
Message
From
13/12/2006 12:09:32
 
 
To
13/12/2006 11:20:54
General information
Forum:
Visual FoxPro
Category:
Installation, Setup and Configuration
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP
Network:
Windows 2003 Server
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01177208
Message ID:
01177244
Views:
19
Hi Chad

I got this to work, but the problem was that (on Windows 2000) the current user needs a particular right, "Act as part of the operating system" which is only granted to Administrators by default..
* Assume the identity of a domain user 
* Based on a FoxTalk article, September 2003, by Alf Borrmann.

define class impersonator as relation

	hidden nUserHandle
	nUserHandle = 0

	procedure init
		this.DeclareAPIs()
	endproc

	procedure destroy
		try
			this.RevertToSelf()
			CloseHandle(this.nUserHandle)
		catch to oDear
		finally
			clear dlls "LogonUser", "ImpersonateLoggedOnUser", "WNetGetUser", "RevertToSelf", "CloseHandle"
		endtry
	endproc


	procedure DeclareAPIs

		#define LOGON32_PROVIDER_DEFAULT 0
		#define LOGON32_LOGON_NETWORK  3

		declare short LogonUser in Win32API;
			string cNewUserName,;
			string cDomainName,;
			string cPassWord,;
			integer nLogonType,;
			integer nLogonProvider,;
			integer @nUserHandle

		declare short ImpersonateLoggedOnUser;
			in Win32API;
			integer nUserHandle

		declare integer WNetGetUser in Win32API;
			string @cName,;
			string @cUser,;
			integer @nBuffersize

		declare short RevertToSelf in Win32API

		declare short CloseHandle in Win32API integer

		* Don't release GetLastError as other modules use it.
		declare integer GetLastError in kernel32

	endproc

	procedure GetLoggedOnUser as string

		local cName as string, nBuffersize as integer, cUser as string
		cName= chr(0)
		nBuffersize = 64
		cUser = replicate(cName, nBuffersize)

		if WNetGetUser(@cName, @cUser, @nBuffersize) = 0
			cUser = left(cUser, at(chr(0), cUser) - 1)
		else
			cUser = "ERROR"
		endif

		return cUser

	endproc

	procedure Impersonate(cDomainName as string, cUsername as string, cPassWord as string) as Boolean

		local nUserHandle as integer, nSuccess as integer, lSuccess as Boolean

		nUserHandle = 0

		nSuccess = LogonUser(cUsername, cDomainName, cPassWord, ;
			LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, @nUserHandle)

		if nSuccess = 0
			error "LogonUser call failed with a code of " + transform(GetLastError())
			this.RevertToSelf()
			return .f.
		endif

		* Store returned handle to class property
		this.nUserHandle = nUserHandle

		nSuccess = ImpersonateLoggedOnUser(nUserHandle)

		if nSuccess = 0
			error "ImpersonateLoggedOnUser call failed with a code of " + transform(GetLastError())
			this.RevertToSelf()
			return .f.
		endif

		* Store returned handle to class property for later release
		this.nUserHandle = nUserHandle

		lSuccess = (upper(this.GetLoggedOnUser()) == upper(cUsername))

		return lSuccess

	endproc

	procedure RevertToSelf()
		local nSuccess
		* RevertToSelf returns zero for failure.
		nSuccess = RevertToSelf()
		return (nSuccess # 0)
	endproc


enddefine
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform