本文介绍了从网络计算机访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要使用计算机的用户名和密码从网络中的另一台计算机访问文件.

有人可以帮我吗?

Saju

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

推荐答案

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



这篇关于从网络计算机访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 11:27