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」を使用しないのはなぜですか。これは、シェルを使用してドラッグ アンド ドロップなどを行う方法を (とりわけ) 示しています。

シェルを使えば、ログオン方法など、さまざまな状況に対応するために必要な 1000 のことを考える必要はありません。

「Windows API Code Pack 1.1」パッケージをダウンロードして、DragAndDropWindow サンプルまたはその他のサンプルを探します。

于 2012-10-10T15:10:41.467 に答える