473,671 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)fil e 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(UserN ame, HostName, Password, 2, 0, ref tokenHandle);

WindowsIdentity newId = new WindowsIdentity (tokenHandle);
WindowsImperson ationContext newUser = newId.Impersona te();

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

Nov 17 '05 #1
4 3874
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.c om> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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)fil e 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(UserN ame, HostName, Password, 2, 0, ref tokenHandle);

WindowsIdentity newId = new WindowsIdentity (tokenHandle);
WindowsImperson ationContext newUser = newId.Impersona te();

try
{
File.Copy(fileP ath,"\\\\" + 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_N EW_CREDENTIALS as dwLogonType (value 3) and the
LOGON32_PROVIDE R_WINNT50 dwLogonProvider (value 3).

Willy.

..
"Ulf" <wa******@aol.c om> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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)fil e 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(UserN ame, HostName, Password, 2, 0, ref tokenHandle);

WindowsIdentity newId = new WindowsIdentity (tokenHandle);
WindowsImperson ationContext newUser = newId.Impersona te();

try
{
File.Copy(fileP ath,"\\\\" + 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(UserN ame, 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/dwLongonProvide r
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.c om> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Hi Willy,

thx for your advice, always appreciated. I changed my code to
LogonUser(UserN ame, 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/dwLongonProvide r
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
261
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 = CreateObject("WbemScripting.SWbemLocator") objService = objLocator.ConnectServer(ComputerName, "root\default", _ strUserName, _ strPassword) objService.Security_.ImpersonationLevel = 3
3
2746
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 to a remote computer. do i have to copy them from the local computer to the remote computer then delete them fromt the local computer after undo-ing the impersonation? Or is there a way to retain access to both the local computer and the remote...
2
5596
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 what I've got (and found through all of my research as the necessary code): Type remoteType = Type.GetTypeFromProgID(r2wprogid, r2wserver) ; object remoteObject = Activator.CreateInstance(remoteType, true) ; The call to the...
3
2235
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 running on a w2k3ee sp1 server 2) I am behind a corporate firewall, but was able to get our network services group to punch a hole at port 80 through. 3) I'm running the webservice using an impersonation to the service account and have the pool...
4
14461
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( "Microsoft.Update.Session", "proact" ); UpdateSession UpdSess = (UpdateSession) Activator.CreateInstance(t); This code works quite well, as long as the user you are currently authenticated to also has administrative access on the target device. This
2
2892
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 worker process runs as the local ASP.NET account. When the application is about to access the remote folder I switch the security context to the remote user programmatically, then switch it back afterwards. When I run the application on my local...
2
1623
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 program that needs to access folders on other machines. But all I get is "permission denied". The popup box doesn't appear. I would guess that the remote machine is challenging my app, but the app isn't responding (???). What I want to do is be...
0
13740
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 until it's finished. If the command didn't terminate within the timeout, the command is killed. (Ex. 2) - launch the command and continue your processing after the command has been started remotely (Ex. 3).
4
7421
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 user and password, and select a storage location (for some files to be created later). I've seen that WMI has Win32_Directory, but I don't know how find which files are contained in a specific directory. I was thinking of using a query select which...
0
8483
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8824
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8603
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8673
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7444
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2060
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.