微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用模拟和 CopyFileEx 从内核 DLL 复制文件?

如何解决使用模拟和 CopyFileEx 从内核 DLL 复制文件?

我正在使用以下代码文件复制到只有一个 X 用户可以访问的目录,我正在使用模拟,但是当我使用它时 copyFileEx 不起作用,但我不知道为什么。如果我删除了模拟部分,它可以正常工作,但我需要将它与用户 X 一起复制,因为在生产中它必须是这样的。

Date.Now()

解决方法

这里是我以前写的一个类,用来做Impersonation下的操作, 查看令牌是如何在 DoWorkUnderImpersonation() 中创建的 将凭据和所需常量设置为 LogonUser()advapi32.dll
所需的操作在 DoWork() 方法中进行,在那里添加您的复制文件逻辑。
从外部调用静态方法DoWorkUnderImpersonation()

// Implementation of the Impersonation class
Impersonation.DoWorkUnderImpersonation("DOMAIN","USER","PASSWORD");


public static class Impersonation
{
    // obtains user token
    [DllImport("advapi32.dll",SetLastError = true)]
    public static extern bool LogonUser(string pszUsername,string pszDomain,string pszPassword,int dwLogonType,int dwLogonProvider,ref IntPtr phToken);

    // closes open handes returned by LogonUser
    [DllImport("kernel32.dll",CharSet = CharSet.Auto)]
    public extern static bool CloseHandle(IntPtr handle);

    public static void DoWorkUnderImpersonation(string _domain,string _userName,string _password)
    {
        //elevate privileges before doing file copy to handle domain security
        WindowsImpersonationContext impersonationContext = null;
        IntPtr userHandle = IntPtr.Zero;
        const int LOGON32_PROVIDER_DEFAULT = 0;
        const int LOGON32_LOGON_INTERACTIVE = 2;
        string domain = _domain;
        string user = _userName;
        string password = _password;

        try
        {
            Debug.WriteLine("windows identify before impersonation: " + WindowsIdentity.GetCurrent().Name);

            // if domain name was blank,assume local machine
            if (domain == "")
                domain = System.Environment.MachineName;

            // Call LogonUser to get a token for the user
            bool loggedOn = LogonUser(user,domain,password,LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT,ref userHandle);

            if (!loggedOn)
            {
                Debug.WriteLine("Exception impersonating user,error code: " + Marshal.GetLastWin32Error());
                return;
            }

            // Begin impersonating the user
            using (impersonationContext = WindowsIdentity.Impersonate(userHandle))
            {
                Debug.WriteLine("Main() windows identify after impersonation: " + WindowsIdentity.GetCurrent().Name);
                //run the program with elevated privileges (like file copying from a domain server)
                DoWork();
            }

        }
        catch (Exception ex)
        {
            Console.Write("Exception impersonating user: " + ex.Message);
        }
        finally
        {
            // Clean up
            if (impersonationContext != null)
            {
                impersonationContext.Undo();
            }

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


    private static void DoWork()
    {
        try
        {
            // MAKE YOUR REQUIRED TASK HERE UNDER IMPERSONATION
        }
        catch (Exception ex)
        {
            Console.Write("error in Impersonation.DoWork() executing a task: " + ex.Message);
        }

    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。