Re: CopyFile across network / logon

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Chris P. [MVP] (msdn_at_chrisnet.net)
Date: 08/19/04


Date: Thu, 19 Aug 2004 12:37:15 -0400

Chris P. [MVP] wrote:
> flox wrote:
>> Hi!
>>
>> I have a problem using CopyFile from one PC to another:
>>
>> I get a "unknown user..." error. Only after accessing the destination
>> PC using Explorer and entering user/password I can call CopyFile
>> without error. I am accessing W2K and NT workstations from a XP or
>> W2KTS system, there is NO logon box when my program calls CopyFile...
>>
>> This is ok as only authenticated users should be able to copy files
>> but using Explorer to enter user/password definitely is not a very
>> good solution...
>>
>> Unfortunately I CANNOT create the same users on both systems due to
>> security restrictions, but I have the user/password for the low
>> security systems I want to copy files to. The problem is that there
>> is no version of CopyFile taking user/password (of the remote machine
>> I want to copy to) what can I do?
>
> The thread that is going to do the copy can authenticate itself with
> the destination (server) using WNetAddConnection2() or equivilant.
> You can do it completely hidden from the user if you know the
> username and password at run time or you can prompt the user if
> necessary by using the CONNECT_INTERACTIVE flag. You don't need to
> map a local drive, set that field to NULL.
>
> Here's an example.

Sorry, hit the wrong key. Here's an example that assumes you know the
username and password:

char path[80];
 UINT error=0;
 NETRESOURCE nr;
 CString Password = password;

 memset(&nr, 0, sizeof(NETRESOURCE));

 if (*server_path)
  sprintf(path, "\\\\%s\\%s",server_name, server_path);
 else
  sprintf(path, "\\\\%s",server_name);

 nr.dwType = RESOURCETYPE_DISK;
 nr.lpLocalName = NULL;
 nr.lpRemoteName = path;
 nr.lpProvider = NULL;

//retryconnect:
 if (*username == 0)
 {
  if (*password == 0)
   error = WNetAddConnection2(&nr, NULL, NULL, 0);
  else
   error = WNetAddConnection2(&nr, Password, NULL, 0);
 }
 else
  error = WNetAddConnection2(&nr, Password, username, 0);



Relevant Pages

  • Re: Intranet Security
    ... Your users are granted access via "Authenticated Users". ... I have the website pointing to a network share on another server. ... I have also tried moving the files to a folder on the IIS server. ... prompted for a username and password. ...
    (microsoft.public.inetserver.iis.security)
  • Re: Forms Authentication and Active Directory
    ... Save their password in the session right after they log in, it's safe there. ... > authenticated users, not Forms Authenticated users. ... > impersonating the already impersonated user! ... >> this simply the username that is logged in. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: shares too visible
    ... With the share permissions set to "Authenticated Users" having read ... Do you use the same username and password on your laptop as you have on the ... domain, not the same account, just the same username/password combo? ... hence the logon prompt at the member servers. ...
    (microsoft.public.windows.server.security)