file handling on remote machine

  • Thread starter Thread starter shamresh
  • Start date Start date
S

shamresh

Hi,

I would like to access another machine on the network, browse some
folders and do some processing on particular files.

I am not sure what api/classes I have to use to allow me to log into
the machine.

I am using C#.

Sham.
 
Is this something that you could do with accessing shares on the "server"
computer, or do you need to have the application doing the processing
running in memory on the "server"?

Jay Taplin MCP
 
I would like to run the application on my machine and bascially log
onto a machine on the network to do some processing. I would prefer to
have the .Net framework on my machine and not on each remote machine.

Sham.
 
Hi there,

Use impersonation + WINAPI:
-- BEGIN CODE --
using System;
using System.Runtime.InteropServices;

public sealed class Impersonation
{
private const int LOGON32_LOGON_INTERACTIVE = 2;
private const int LOGON32_LOGON_NETWORK_CLEARTEXT = 4;
private const int LOGON32_PROVIDER_DEFAULT = 0;

private enum SECURITY_IMPERSONATION_LEVEL : int
{
SecurityAnonymous = 0,
SecurityIdentification = 1,
SecurityImpersonation = 2,
SecurityDelegation = 3
}

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private extern static int DuplicateToken(
IntPtr hToken,
SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
ref IntPtr hNewToken);

[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
private static extern int LogonUser(
string lpszUserName,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);

private static System.Security.Principal.WindowsImpersonationContext
m_oContext = null;

public static bool Login(string strLogin, string strPassword, string
strDomain)
{
IntPtr hTokenDuplicate = IntPtr.Zero;
IntPtr hToken = IntPtr.Zero;

if (LogonUser(strLogin, strDomain, strPassword, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref hToken) != 0)
{
if (DuplicateToken(hToken,
SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, ref hTokenDuplicate) !=
0)
{
m_oContext = new
System.Security.Principal.WindowsIdentity(hTokenDuplicate).Impersonate();
return (m_oContext != null);
}
}
return false;
}

public static void Logout()
{
if (m_oContext != null)
m_oContext.Undo();
}
}

-- END CODE --
Hope this helps
 
Back
Top