Solved

Remote login

Posted on 2002-07-20
18
597 Views
Last Modified: 2013-12-03
Hi,
  I need to connect to a machine programtically as a specific user and retrieve the contents of a file. Can anyone suggest which API would be appropriate? LogonUser() API creates a session on the localhost, whereas I want to login to the server as a specific user.

Sample code would be appreciated.

thanks in advance.


0
Comment
Question by:rajani_n
  • 6
  • 5
  • 4
  • +1
18 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7168015
Use "LogonUser()" - it also works remotely. All you have to do is specify "<machine>\<username" as the UserId.
0
 

Author Comment

by:rajani_n
ID: 7168600
I tried providing <machine>\<username>. however it does not seem to be working. Any thing else i need to do?
0
 
LVL 3

Expert Comment

by:jimbucci
ID: 7169008
So you basically want to connect to a remote computer, open a file, do whatever you have to do, close the file, and disconnect.
I would use the WNetAddConnection2() API.  Here is some code:

BOOL connect(string servername, string share
             string user, string pw)
{
   DWORD dwConnectStatus;
   BOOL     bResult = FALSE;  // assume failure
   char     fullpath[50] = {0};
   int     x;
   NETRESOURCE     n;  // network resource struct

   ZeroMemory( &n, sizeof( NETRESOURCE ) );

   // concat server and share for remote name
   wsprintf( fullpath, "%s%s", server,sharename );

   // set network resource
   n.dwType = RESOURCETYPE_DISK;
   n.lpRemoteName = (LPTSTR) fullpath;
   n.lpLocalName =  (LPTSTR) driveletter;

   print("Attempting to connect to network");

   // Only loop max 10 times - we can't wait forever!
   for( x=0; x<10; x++ )
   {
      // attempt connection
      dwConnectStatus =
         WNetAddConnection2( &n, password, username, 0 );

      if( dwConnectStatus == NO_ERROR )
      {
         bResult = TRUE;
      break;
      }

      // non-fatal, disconnect and try again
      if( dwConnectStatus == ERROR_ALREADY_ASSIGNED )
     dwConnectStatus =
        WNetCancelConnection2( driveletter,  
                    CONNECT_UPDATE_PROFILE, TRUE );
     else
     {
     // connection error - log and exit
     LogError(dwConnectStatus, n);    
          break;
     }
   }
     
   // set state
   bConnected = bResult;

   return bResult;
}

Open the file as you would any other file...

Call:
WNetCancelConnection2( driveletter,  
                       CONNECT_UPDATE_PROFILE, TRUE );

when you are done.

Let me know if this helps.
Jim
0
 
LVL 86

Expert Comment

by:jkr
ID: 7169391
>>however it does not seem to be working

What error do you get?
0
 

Author Comment

by:rajani_n
ID: 7169792
I tried using the code provided by Jim, I get an error 1219(The credentials supplied conflict with an existing set of credentials.) when I supply the username and pwd to be different than the one who has logged into the remote system. However, the code works if I supply the UserId and password of the user who has logged into remote system.

My apllication will be providing a random user.

0
 
LVL 86

Expert Comment

by:jkr
ID: 7169811
Well, what error do you get with 'LogonUser()'? Do you want to _connect_ to a remote machine or do a _logon_?
0
 
LVL 3

Expert Comment

by:jimbucci
ID: 7170227
That error occurs if are already attached to the remote computer as another user.  Windows only allows one login per remote connection.  So either disconnect from the remote computer, or use your login/password.
When I had to do this, the local machine did not have any connections to the remote computer until the WNetAddConnection2() call.  
If your app provides a random user, you just have to make sure that the user is on the system and has access to the files.

jim
0
 

Author Comment

by:rajani_n
ID: 7173545
LogonUser(..) call fails by returning false when I provider <machine>/<username>.

My requirement is as follows: I need to access a share on a remote machine that has specific user permissions for read. From my system, I will provide the username/password(these may be different from the currenly logged in user) to access the share and should be able to access the files located in that share.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:jimbucci
ID: 7173896
You cannot use LogonUser() to connect to a remote computer.  

From MSDN:
The LogonUser function attempts to log a user on to the local computer, that is, to the computer from which LogonUser was called. You cannot use LogonUser to log on to a remote computer... etc.

As per your requirements, WNetAddConnection2() will do all of that for you.  You must remember the credentials conflict when using it - no mapped drives or connections when connecting as someone else.

The code I provided does work.  You can remove the looping since that was for a specific purpose.  One call should determine if your connection was valid.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7174276
>>You cannot use LogonUser() to connect to a remote computer.  

Yes, you can. The terminology is misleading here, as a little lower it states

LOGON32_LOGON_INTERACTIVE

This logon type is intended for users who will be interactively using the machine, such as a user being logged on by a terminal server, remote shell, or similar process.  

'LogonUser()' doesn't do a graphical logon of course, but creates a user token.
0
 

Author Comment

by:rajani_n
ID: 7185572
Hi,
  I found that NetUseAdd(.) api allows to login to remote computer from documentation. Can anyone provide me with the work code? I tried using it, however m geting an "access voilation" error. All the other APIs like NetGetDCName(..) works fine for me.

Any help will be appreciated.

Thanks
Rajani
0
 
LVL 86

Expert Comment

by:jkr
ID: 7185666
See http://www.mvps.org/win32/network/nusea.cpp for a fully featured sample on 'NetUseAdd()'
0
 

Author Comment

by:rajani_n
ID: 7190777
Hi Jim,
   after making the connection, I tried retiving the file by specifying the UNC name, "\\\\server\\share\\file". However, it does not seem to be working. Can you please helpe me figure out what the problem could be.

I am using the code supplied by you. Initially, it did not work as I had a mapped connection on my m/c. I took it off and it works.

thanks
Rajani
0
 
LVL 3

Expert Comment

by:jimbucci
ID: 7190799
After making the connection you should have a drive letter for the remote connection.  Use the drive instead of the UNC name.

Jim
0
 

Author Comment

by:rajani_n
ID: 7190801
Also I wanted to know if WNetAddConnection* APIs or NetUseAdd APIs are used, how can we maintain the sessions. I don't get any handle using these APIs. Also, I don't want to use the drive name, it will be NULL. So if I open some 5 connections at a time, how will I know which one was for which of the connections?

0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10787682
PAQed, with points refunded (50)

modulo
Community Support Moderator
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now