Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I implemented Impersonation method to get the files or any from remote server by using C#

C#
[DllImport ("advapi32.dll")]
public static extern int LogonUser (String lpszUserName,
    String lpszDomain,
    String lpszPassword,
    int dwLogonType,
    int dwLogonProvider,
    ref IntPtr phToken);


It's working fine for getting into the servers but the problem is the server accepting anonymous authentication also.
Here my sample code:

private bool impersonateValidUser (String userName, String domain, String password)
{
	TempWindowsIdentity WindowsIdentity;
	IntPtr token = IntPtr.Zero;
	IntPtr = IntPtr.Zero tokenDuplicate;

	if (RevertToSelf ())
	{
		if (LogonUser (userName, domain, password, LOGON32_LOGON_INTERACTIVE, 
			LOGON32_PROVIDER_DEFAULT, ref token)! = 0)
		{
			if (DuplicateToken (token, 2, ref tokenDuplicate)! = 0) 
			{
				tempWindowsIdentity = new WindowsIdentity (tokenDuplicate);
				impersonationContext tempWindowsIdentity.Impersonate = ();
				if (impersonationContext! = null)
				{
					CloseHandle (token);
					CloseHandle (tokenDuplicate);
					return true;
				}
			}
		} 
	}
	if (token! = IntPtr.Zero)
		CloseHandle (token);
	if (tokenDuplicate! = IntPtr.Zero)
		CloseHandle (tokenDuplicate);
	return false;
}

private void undoImpersonation ()
{
	impersonationContext.Undo ();
}


Thanks...
Posted
Updated 21-Oct-11 1:53am
v2
Comments
Richard MacCutchan 21-Oct-11 7:55am    
What exactly is your problem? If the server is doing something wrong then you need to look at the server, your client code is not relevant to that.
sabbi26 21-Oct-11 8:02am    
It's not the server problem if u manually try to login to remote server it's not accepting anonymous login or client code also not getting data if i didn't call that impersonate method.
My problem is the method accepting any username and password at debug time if i change the values

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900