473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

remote impersonation issue

Ulf
Hi all,

I run into some problems when trying to copy a file to a remote
machine.
I first create with WMI a share on the remote machine (works fine),
then impersonate with admin rights and try to copy the file using the
File.Copy method. But the exception always returns a "(local)file not
found" message. Could someone give me a hint where I make a mistake in
the following code ? many thx in advance :)

Ulf

code :

IntPtr tokenHandle = new IntPtr(0);
tokenHandle = IntPtr.Zero;
LogonUser(UserName, HostName, Password, 2, 0, ref tokenHandle);

WindowsIdentity newId = new WindowsIdentity(tokenHandle);
WindowsImpersonationContext newUser = newId.Impersonate();

try
{
File.Copy(filePath,"\\\\" + HostName + "\\" + shareName + "\\" +
fileName,true);
}
catch(Exception copyEx)
{
MessageBox.Show("Copy failure\n" + copyEx.Message,HostName);
}
finally
{
newUser.Undo();
}

Nov 17 '05 #1
4 3864
Hi,

Please make sure the user whom you impresonate has enough rights to access
the file on the local machine.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Ulf" <wa******@aol.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi all,

I run into some problems when trying to copy a file to a remote
machine.
I first create with WMI a share on the remote machine (works fine),
then impersonate with admin rights and try to copy the file using the
File.Copy method. But the exception always returns a "(local)file not
found" message. Could someone give me a hint where I make a mistake in
the following code ? many thx in advance :)

Ulf

code :

IntPtr tokenHandle = new IntPtr(0);
tokenHandle = IntPtr.Zero;
LogonUser(UserName, HostName, Password, 2, 0, ref tokenHandle);

WindowsIdentity newId = new WindowsIdentity(tokenHandle);
WindowsImpersonationContext newUser = newId.Impersonate();

try
{
File.Copy(filePath,"\\\\" + HostName + "\\" + shareName + "\\" +
fileName,true);
}
catch(Exception copyEx)
{
MessageBox.Show("Copy failure\n" + copyEx.Message,HostName);
}
finally
{
newUser.Undo();
}


Nov 17 '05 #2
You are impersonating to gain access to the remote resource, but the
impersonated user is not a local account so you are screwed.
One way to solve this, provided you are running XP or higher, is to create a
logon token with "split identity", that is using
LOGON32_LOGON_NEW_CREDENTIALS as dwLogonType (value 3) and the
LOGON32_PROVIDER_WINNT50 dwLogonProvider (value 3).

Willy.

..
"Ulf" <wa******@aol.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi all,

I run into some problems when trying to copy a file to a remote
machine.
I first create with WMI a share on the remote machine (works fine),
then impersonate with admin rights and try to copy the file using the
File.Copy method. But the exception always returns a "(local)file not
found" message. Could someone give me a hint where I make a mistake in
the following code ? many thx in advance :)

Ulf

code :

IntPtr tokenHandle = new IntPtr(0);
tokenHandle = IntPtr.Zero;
LogonUser(UserName, HostName, Password, 2, 0, ref tokenHandle);

WindowsIdentity newId = new WindowsIdentity(tokenHandle);
WindowsImpersonationContext newUser = newId.Impersonate();

try
{
File.Copy(filePath,"\\\\" + HostName + "\\" + shareName + "\\" +
fileName,true);
}
catch(Exception copyEx)
{
MessageBox.Show("Copy failure\n" + copyEx.Message,HostName);
}
finally
{
newUser.Undo();
}

Nov 17 '05 #3
Ulf
Hi Willy,

thx for your advice, always appreciated. I changed my code to
LogonUser(UserName, HostName, Password, 9, 3, ref tokenHandle);
which works fine as long as I connect from XP to XP boxes. But when the
local machine is W2K and the remote one XP, I always get Logon
failures. I tried several different dwLogonType/dwLongonProvider
combinations, but with no success. Is there something special to be
done when connecting from W2K to XP ?

thx,

Ulf

Nov 17 '05 #4
That's why I said XP and higher only, that means it's not supprted on W2K.
The only way to get this done is by using a domain account logon, but I
guess you don't run in a domain, so the only thing which is left is to
establish a network session with the remote share or remote server using
"net use" command line command.

net use \\server\share password /user:username
where username and password are the remote users credentials.
Once you have this use record in your logon session you can simply copy
files from local disks to the remote share using the UNC path.

Willy.

"Ulf" <wa******@aol.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi Willy,

thx for your advice, always appreciated. I changed my code to
LogonUser(UserName, HostName, Password, 9, 3, ref tokenHandle);
which works fine as long as I connect from XP to XP boxes. But when the
local machine is W2K and the remote one XP, I always get Logon
failures. I tried several different dwLogonType/dwLongonProvider
combinations, but with no success. Is there something special to be
done when connecting from W2K to XP ?

thx,

Ulf

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: steven chong | last post by:
hi everyone, I need to do a query into some remote registry, workstation PCs as well as domain PCs. Previously, i was using the native WMI COM objects like this: objLocator =...
3
by: Daniel | last post by:
Is it possible to retain local file system read, write, delete access while impersonating for access to a remote drive in a different domain? I need to be able to move files from a local computer...
2
by: Bryan Tubbs | last post by:
I'm having trouble connecting to a remote COM object using Reflection. I've got a Web Form using C# that needs to connect to a COM object on a remote server. The snippet of code that follows is...
3
by: todd_groten | last post by:
Alright, I've been searching around for some time and have not been able to find anything concrete on fixing an issue I am having. Here's the situation: 1) I have a non-anonymous webservice...
4
by: Heliotic | last post by:
Hi everyone, Currently I am working on an application that will perform a remote scan of a specified server using the following code: Type t = Type.GetTypeFromProgID(...
2
by: Jon L. Lovesky | last post by:
Hello all, I am attempting to access a remote folder from an asp.net application (all within the same domain). The application is configured for windows authentication in IIS and the asp.net...
2
by: Terry Olsen | last post by:
I've been everywhere, but can't seem to find a solution to my problem.... When you browse to a folder on another machine, it challenges you for your ID & Password (popup box). I'm writing a...
0
by: Patrick A. | last post by:
Dll written in VB.NET 2003 to start a command remotely. You can : - launch the command and wait until it's finished. (Ex. 1) - launch the command providing a timeout in seconds, it will wait...
4
by: CosminB [BRT] | last post by:
Hi, How can this be done? I've searched the internet for a solution to this problem, and can't find a clear answer. I need to be able to browse a remote computer's filesystem, using a certain...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.