0

我的目标是用 c# 将文件夹从我的系统复制到远程计算机。我到处搜索,找到了一些关于如何做到这一点的信息。我正在使用域、用户名和密码调用 LogonUser 函数,但它失败并返回 0。

下面是我的一段代码。你能帮我找出问题所在吗?

class Program
{
    #region Assembly Functions
    [DllImport("advapi32.dll")]
    public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword,
        int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);

    [DllImport("kernel32.dll")]
    public static extern bool CloseHandle(IntPtr handle);
    #endregion

    static void Main(string[] args)
    {
        SafeTokenHandle safeTokenHandle;
        WindowsImpersonationContext impersonatedUser = null;
        WindowsIdentity newId;
        IntPtr tokenHandle;
        IntPtr userHandle = IntPtr.Zero;
        tokenHandle = IntPtr.Zero;

        Console.WriteLine("Enter your domain.");
        string domain = Console.ReadLine();
        Console.WriteLine("Enter you user name.");
        string uname = Console.ReadLine();
        Console.WriteLine("Enter your password (Caution, password won't be hidden).");
        string password = Console.ReadLine();

        const int LOGON32_PROVIDER_DEFAULT = 0;
        //This parameter causes LogonUser to create a primary token. 
        const int LOGON32_LOGON_INTERACTIVE = 2;
        bool logon = LogonUser(uname, domain, password,
            LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle);            

        if (false == logon)
        {
            int ret = Marshal.GetLastWin32Error();
            Console.WriteLine("LogonUser failed with error code : {0}", ret);
            throw new System.ComponentModel.Win32Exception(ret);
        }

        if (logon)
        {
            newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
            using (impersonatedUser = newId.Impersonate())
            {
                // Check the identity.
                Console.WriteLine("After impersonation: "
                    + WindowsIdentity.GetCurrent().Name);
            }
            File.Copy(@"c:\result.xml", @"C:\result.xml", true);
        }

        //Undo impersonation
        if (impersonatedUser != null)
        {
            impersonatedUser.Undo();
        }

        if (tokenHandle != IntPtr.Zero)
        {
            CloseHandle(tokenHandle);
        }
    }
}



public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
{
    private SafeTokenHandle()
        : base(true)
    {
    }

    [DllImport("kernel32.dll")]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [SuppressUnmanagedCodeSecurity]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool CloseHandle(IntPtr handle);

    protected override bool ReleaseHandle()
    {
        return CloseHandle(handle);
    }
}
4

1 回答 1

-2

为什么不使用“Windows API Code Pack 1.1”,它展示了(除其他外)如何使用 shell 来执行拖放操作——这实际上是您尝试做的事情。

如果使用 Shell,您不必考虑如何登录以及支持各种情况所需的 1000 件其他事情。

下载“Windows API Code Pack 1.1”包并查找 DragAndDropWindow 示例或其他一些示例。

于 2012-10-10T15:10:41.467 回答