Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi ,

I need to access a file from another computer in the network using the machine's user ID and password.

Can anyone please help me out in this?

Saju
Posted

1 solution

This can help you http://support.microsoft.com/kb/306158:

I have taken this piece of code from there, and it works:

C#
public class Program {
    public const int LOGON32_LOGON_INTERACTIVE = 2;
    public const int LOGON32_PROVIDER_DEFAULT = 0;

    [DllImport("advapi32.dll")]
    public static extern int LogonUserA(String lpszUserName,
        String lpszDomain,
        String lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken);
    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int DuplicateToken(IntPtr hToken,
        int impersonationLevel,
        ref IntPtr hNewToken);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool RevertToSelf();

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern bool CloseHandle(IntPtr handle);

    static void Main(string[] args) {
        WindowsImpersonationContext impersonationContext = null;

        WindowsIdentity tempWindowsIdentity;
        IntPtr token = IntPtr.Zero;
        IntPtr tokenDuplicate = IntPtr.Zero;

        //if (RevertToSelf()) {
            if (LogonUserA(@"RemoteUserName", "YourRemotePC", "YourPassword", LOGON32_LOGON_INTERACTIVE,
                LOGON32_PROVIDER_DEFAULT, ref token) != 0) {
                if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) {
                    tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                    impersonationContext = tempWindowsIdentity.Impersonate();
                    if (impersonationContext != null) {
                        CloseHandle(token);
                        CloseHandle(tokenDuplicate);
                    }
                } else {
                    Console.WriteLine("DuplicateToken error");
                    return;
                }
            } else {
                Console.WriteLine("LogonUser error");
                return;
            }
        //}
        if (token != IntPtr.Zero)
            CloseHandle(token);
        if (tokenDuplicate != IntPtr.Zero)
            CloseHandle(tokenDuplicate);

        Console.WriteLine("Logged in");
        Console.ReadKey();
        Console.WriteLine(File.ReadAllText(@"\\YourRemotePC\C$\Windows\system32\drivers\etc\hosts")); // Read the file here

        impersonationContext.Undo();
}
 
Share this answer
 
Comments
El_Codero 5-Mar-12 18:34pm    
Bookmarked your 5-Stars Link. Best Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900