472,950 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

File Copy from a Remote Server

Hello!

I'm trying to copy a file from another computer on the network that I do not
have permission with my current logon details to access.

If I open the folder using the Windows file manager with the path "\\
192.168.2.2\temp" (where temp is a shared directory on server \\192.168.2.2),
windows prompts for a User Name and password of a user who has permission on
that computer to access that directory. If I enter valid details, the folder
is opened and I can copy files from this machine.

How do I do this programmatically in c++ or c#? Standard file open and/or
directory commands do not have the option to enter a valid Username/password
and they just fails with permission denied (understandably).

Regards....Adrian

Jul 31 '08 #1
5 17502
On Thu, 31 Jul 2008 12:42:03 -0700, AdrianMorris
<Ad**********@discussions.microsoft.comwrote:
[...]
If I open the folder using the Windows file manager with the path "\\
192.168.2.2\temp" (where temp is a shared directory on server
\\192.168.2.2),
windows prompts for a User Name and password of a user who has
permission on
that computer to access that directory. If I enter valid details, the
folder
is opened and I can copy files from this machine.

How do I do this programmatically in c++ or c#? Standard file open and/or
directory commands do not have the option to enter a valid
Username/password
and they just fails with permission denied (understandably).
I don't know if .NET exposes this directly. Possibly via WMI. One way or
the other though, I know that if you execute a "net use \\<servername>
/user:<username/password:<password>" command, then connections to shares
on the server will use those credentials.

In fact, I often do this from a command-prompt before attaching to
protected shares just because it winds up being faster than waiting for
Windows to figure out it needs to prompt me. :)

Pete
Jul 31 '08 #2
I'm trying to copy a file from another computer on the network that I do
not
have permission with my current logon details to access.

If I open the folder using the Windows file manager with the path "\\
192.168.2.2\temp" (where temp is a shared directory on server
\\192.168.2.2),
windows prompts for a User Name and password of a user who has permission
on
that computer to access that directory. If I enter valid details, the
folder
is opened and I can copy files from this machine.

How do I do this programmatically in c++ or c#? Standard file open and/or
directory commands do not have the option to enter a valid
Username/password
and they just fails with permission denied (understandably).
In C++ you can use the "NetUseAdd()" family of functions or
"WNetAddConnection2()" and cousins. There's probably a wrapper in .NET
somewhere for this, possibly using WMI. Understanding authentication is
really the crux of the issue but that's a deep subject and there are
(potential) trip wires along the way. I can post a primer on basic
authentication if you want however (less than a page). I wrote it for a
colleague a few years back and it will give you an entry-level
understanding. You'll still have to do further research before you can
safely tackle these functions though (and note that just copying a posted
example using these functions or anything higher you might find typically
won't cut it if you want bullet-proof code).
Jul 31 '08 #3
Hello Larry,

Thanks for these pointers...

If you have any further information, I would really appreciate it!

Regards....Adrian

"Larry Smith" wrote:
I'm trying to copy a file from another computer on the network that I do
not
have permission with my current logon details to access.

If I open the folder using the Windows file manager with the path "\\
192.168.2.2\temp" (where temp is a shared directory on server
\\192.168.2.2),
windows prompts for a User Name and password of a user who has permission
on
that computer to access that directory. If I enter valid details, the
folder
is opened and I can copy files from this machine.

How do I do this programmatically in c++ or c#? Standard file open and/or
directory commands do not have the option to enter a valid
Username/password
and they just fails with permission denied (understandably).

In C++ you can use the "NetUseAdd()" family of functions or
"WNetAddConnection2()" and cousins. There's probably a wrapper in .NET
somewhere for this, possibly using WMI. Understanding authentication is
really the crux of the issue but that's a deep subject and there are
(potential) trip wires along the way. I can post a primer on basic
authentication if you want however (less than a page). I wrote it for a
colleague a few years back and it will give you an entry-level
understanding. You'll still have to do further research before you can
safely tackle these functions though (and note that just copying a posted
example using these functions or anything higher you might find typically
won't cut it if you want bullet-proof code).
Aug 1 '08 #4
Hello Larry,
>
Thanks for these pointers...

If you have any further information, I would really appreciate it!
Ok. The primer I referred to is below. Note that solid info on this stuff
and the SMB protocol under Windows (which is the subject of your post) is
hard to come by IMO. One very good source however is "Programming Windows
Security" by Keith Brown. It's getting old now but still very relevant.
There's a chapter on this very subject in fact. Good luck.

"Here's the (simplified) story (believe it or not). In Windows or any other
OS that implements the
SMB (Server Message Block) protocol (which drives the following scenario),
whenever any
logon session on machine A tries to access a shared resource on any machine
B for the first time
(trying to access a file or folder usually), the file server on machine B
will first try to
authenticate the incoming user so it knows who they are and can therefore
determine what
local resources (file, folders, etc.) they're allowed to access. Note that
the file server runs as a
Windows service and is normally available on all Windows machines (you can
just think of this
as the program that provides access to shared resources on each machine -
"authentication" is
really just the process of logging onto this server for all intents and
purposes). By default the
"user" will be the user associated with the calling thread on machine A and
that normally just
refers to the user who's currently logged onto machine A (though the story
actually runs deeper).
So, for instance, if I log onto "MachineA" as "CA005\lsmith" (my domain
account in the CA005
domain) and I then try to access any file or folder on
"\\MachineB\SomeShare" using Explorer
for instance, the file server on "MachineB" will first try to authenicate
"CA005\lsmith" to
determine that it's really me (otherwise just anyone could come along and
access "MachineB" so
where's the security?). Since I'm a member of a trusted domain ("MachineB"
trusts the CA005
domain controller to vouch for all users in that domain), "MachineB" will
then send my
credentials ("CA005\lsmith") to the CA005 domain controller (where my domain
account is
stored) and the domain controller will then authenticate me accordingly
(verifying that I am in
fact who I claim to be - note that my password is never actually sent across
the wire for security
reasons but how it all works is another story). Once authenticated
(remember, "MachineB" trusts
the CA005 domain controller to do this and authentication is all about
trust), a network logon
session is then created on "MachineB" for "CA005\lsmith" and for all intents
and purposes I'm
now logged onto "MachineB" as if I walked up to it and logged on (even
though I'm still sitting
in front of "MachineA" - note that it's not quite the same as logging onto
"MachineB" directly
since a "network" logon session now exists opposed to an "interactive" logon
session but again,
this is the simple story). Now I can access anything on "MachineB" that
"CA005\lsmith" is
allowed to access and I can do it while still sitting in front of "MachineA"
(so I can now access
all files and folders under "\\MachineB\SomeShare" that "CA005\lsmith" is
allowed to access).
Now, lets' say I log off "MachineA" and logon again using a local account
instead ("swhite" for
instance which is no longer a domain account found on the domain controller
but a local account
created on "MachineA" itself). I'm therefore logged onto "MachineA" as
"MachineA\swhite"
instead of "CA005\lsmith" (all programs I run will then assume this
identity). If I now try to
access "\\MachineB\SomeShare" again, the file server on "MachineB" can no
longer authenticate
me because it now sees someone called "MachineA\swhite" trying to logon and
it doesn't trust
"MachineA" to vouch for "swhite" (like it does the "CA005" domain controller
to vouch for
"lsmith"- the "swhite" account exists on some machine called "MachineA"
which "MachineB"
may have never even heard of and doesn't trust regardless - this is the way
things work in a
Windows domain). "MachineB" will therefore fail to authenticate me and our
old nemesis
"Access Denied" will rear its ugly head on "MachineA" (telling me that
"MachineB" doesn't trust
"MachineA" to vouch for "MachineA\swhite"). To get around this problem you
can generally do
one of three basic things:

1) Create a matching "swhite" account on "MachineB" with the same password
as the "swhite"
account on "MachineA". "MachineB" will then try to authenticate me using the
local account
database (on "MachineB") and so long as the passwords on "MachineA" and
"MachineB" are
kept synchronized, I'll be successfully authenticated (this is how the
Windows file server deals
with accounts whose authorities it doesn't recognize or trust - as long as a
matching account
name and password exist on the local machine, the file server treats the
incoming user as that
particular local user and can log them on) . A network logon session will
then be created as
described earlier but now you'll be running as "MachineB\swhite" instead of
"CA005\lsmith"
(see earlier discussion). You're now effectively logged onto "MachineB" as
if you walked up to it
and logged on as the local "swhite" user (again, a network logon session is
created instead of an
"interactive" logon session however). You can therefore access anything on
that machine that
"MachineB\swhite" is normally allowed to access (even though you're now
doing this on
"MachineA").

2) Activate the "Guest" account on MachineB (disabled by default) and log on
as a "Guest". Let
me know if you want details but it's neither practical nor a secure
alternative.

3) Access "MachineB" using a "NULL Session" but again, it's neither
practical nor secure.

For local accounts, option 1 is therefore your only practical alternative at
a customer site. Also
note that depending on the nature of the application, you can prompt the
user for the credentials
necessary to access MachineB (on the fly) or retrieve them from some secure
source.
"MachineA" can then programatically access "Machine B" using any credentials
it wants (i.e.,
they need not be the credentials of the calling thread which is merely the
default behaviour).
Programming this isn't difficult BTW (once you understand how authentication
works) but let
me know if you want further details (as if this wasn't enough :)"
Aug 1 '08 #5
Hello Larry,

Many thanks for your help.

I used the "LogonUser" followed by the "ImpersonateLoggedOnUser" functions
and all is working well.

It did involve setting up a local user account on the machine running the
program that matches the Username/Password of a suitable user on the remote
machine as well.

I initially banged my head trying to logon to to remote machine....
unsuccessful.... The solution is to Logon locally and that username and
password is then automatically used when the remote access is made, and all
is well.

Kind Regards....Adrian

"Larry Smith" wrote:
Hello Larry,

Thanks for these pointers...

If you have any further information, I would really appreciate it!

Ok. The primer I referred to is below. Note that solid info on this stuff
and the SMB protocol under Windows (which is the subject of your post) is
hard to come by IMO. One very good source however is "Programming Windows
Security" by Keith Brown. It's getting old now but still very relevant.
There's a chapter on this very subject in fact. Good luck.

"Here's the (simplified) story (believe it or not). In Windows or any other
OS that implements the
SMB (Server Message Block) protocol (which drives the following scenario),
whenever any
logon session on machine A tries to access a shared resource on any machine
B for the first time
(trying to access a file or folder usually), the file server on machine B
will first try to
authenticate the incoming user so it knows who they are and can therefore
determine what
local resources (file, folders, etc.) they're allowed to access. Note that
the file server runs as a
Windows service and is normally available on all Windows machines (you can
just think of this
as the program that provides access to shared resources on each machine -
"authentication" is
really just the process of logging onto this server for all intents and
purposes). By default the
"user" will be the user associated with the calling thread on machine A and
that normally just
refers to the user who's currently logged onto machine A (though the story
actually runs deeper).
So, for instance, if I log onto "MachineA" as "CA005\lsmith" (my domain
account in the CA005
domain) and I then try to access any file or folder on
"\\MachineB\SomeShare" using Explorer
for instance, the file server on "MachineB" will first try to authenicate
"CA005\lsmith" to
determine that it's really me (otherwise just anyone could come along and
access "MachineB" so
where's the security?). Since I'm a member of a trusted domain ("MachineB"
trusts the CA005
domain controller to vouch for all users in that domain), "MachineB" will
then send my
credentials ("CA005\lsmith") to the CA005 domain controller (where my domain
account is
stored) and the domain controller will then authenticate me accordingly
(verifying that I am in
fact who I claim to be - note that my password is never actually sent across
the wire for security
reasons but how it all works is another story). Once authenticated
(remember, "MachineB" trusts
the CA005 domain controller to do this and authentication is all about
trust), a network logon
session is then created on "MachineB" for "CA005\lsmith" and for all intents
and purposes I'm
now logged onto "MachineB" as if I walked up to it and logged on (even
though I'm still sitting
in front of "MachineA" - note that it's not quite the same as logging onto
"MachineB" directly
since a "network" logon session now exists opposed to an "interactive" logon
session but again,
this is the simple story). Now I can access anything on "MachineB" that
"CA005\lsmith" is
allowed to access and I can do it while still sitting in front of "MachineA"
(so I can now access
all files and folders under "\\MachineB\SomeShare" that "CA005\lsmith" is
allowed to access).
Now, lets' say I log off "MachineA" and logon again using a local account
instead ("swhite" for
instance which is no longer a domain account found on the domain controller
but a local account
created on "MachineA" itself). I'm therefore logged onto "MachineA" as
"MachineA\swhite"
instead of "CA005\lsmith" (all programs I run will then assume this
identity). If I now try to
access "\\MachineB\SomeShare" again, the file server on "MachineB" can no
longer authenticate
me because it now sees someone called "MachineA\swhite" trying to logon and
it doesn't trust
"MachineA" to vouch for "swhite" (like it does the "CA005" domain controller
to vouch for
"lsmith"- the "swhite" account exists on some machine called "MachineA"
which "MachineB"
may have never even heard of and doesn't trust regardless - this is the way
things work in a
Windows domain). "MachineB" will therefore fail to authenticate me and our
old nemesis
"Access Denied" will rear its ugly head on "MachineA" (telling me that
"MachineB" doesn't trust
"MachineA" to vouch for "MachineA\swhite"). To get around this problem you
can generally do
one of three basic things:

1) Create a matching "swhite" account on "MachineB" with the same password
as the "swhite"
account on "MachineA". "MachineB" will then try to authenticate me using the
local account
database (on "MachineB") and so long as the passwords on "MachineA" and
"MachineB" are
kept synchronized, I'll be successfully authenticated (this is how the
Windows file server deals
with accounts whose authorities it doesn't recognize or trust - as long as a
matching account
name and password exist on the local machine, the file server treats the
incoming user as that
particular local user and can log them on) . A network logon session will
then be created as
described earlier but now you'll be running as "MachineB\swhite" instead of
"CA005\lsmith"
(see earlier discussion). You're now effectively logged onto "MachineB" as
if you walked up to it
and logged on as the local "swhite" user (again, a network logon session is
created instead of an
"interactive" logon session however). You can therefore access anything on
that machine that
"MachineB\swhite" is normally allowed to access (even though you're now
doing this on
"MachineA").

2) Activate the "Guest" account on MachineB (disabled by default) and log on
as a "Guest". Let
me know if you want details but it's neither practical nor a secure
alternative.

3) Access "MachineB" using a "NULL Session" but again, it's neither
practical nor secure.

For local accounts, option 1 is therefore your only practical alternative at
a customer site. Also
note that depending on the nature of the application, you can prompt the
user for the credentials
necessary to access MachineB (on the fly) or retrieve them from some secure
source.
"MachineA" can then programatically access "Machine B" using any credentials
it wants (i.e.,
they need not be the credentials of the calling thread which is merely the
default behaviour).
Programming this isn't difficult BTW (once you understand how authentication
works) but let
me know if you want further details (as if this wasn't enough :)"
Aug 8 '08 #6

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

Similar topics

4
by: Joker | last post by:
How to copy a jpg file on from local computer to remote server on the basis of PHP code?
7
by: Dan V. | last post by:
Situation: I have to connect with my Windows 2000 server using VS.NET 2003 and C# and connect to a remote Linux server at another company's office and query their XML file. Their file may be...
2
by: jake | last post by:
How can I copy a file securely (encryption) from remote computers easily (Mac and Linux) to my Win 2000 Server once a day?
1
by: POnfri | last post by:
Hi, I have a problem in a peace of code were i'm doing a file copy using File.Copy. The Source is local and the target is a remote machine. Example: File.Copy(C:\temp\hi.txt,...
2
by: Stu | last post by:
Using IIs 6.0 on a Server 2003 box, and using ASP.NET I'm trying to do the following code snippit... Dim NewName As String = "\\network_share_path\edit_me.ppt" Dim PubName As String =...
2
by: Glen Conway | last post by:
Hi, I am trying to get the contents of a file in a hidden share on a remote server, something like '\\server.domain.com\c$\program files\application\document.xml'. When I try any of the...
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
1
by: Mark | last post by:
The new file based web server that comes with Visual Studio 2005 allows you to develop and debug an ASP.NET on a remote computer rather than having to rely on IIS. Assuming you've got decent LAN...
5
by: twiggy182 | last post by:
Hi, I really need you help because I'm not very familliar with ASP and I could not find any solution to my problem. To put you in situation, I have a CGI to which I send a file name, and that...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.