【问题标题】:Impersonation for file copy across network模拟跨网络文件复制
【发布时间】:2011-12-30 06:46:53
【问题描述】:

我想从同一域中的远程计算机复制文件。所以我正在使用模仿来做到这一点。

我正在使用 advapi32.dll 的 DLLImport,它正确地模拟了用户。

现在执行下面的代码行时出现以下错误。

\\line

File.Copy(@"\\sins00048178\D$\BNCustody\Swift\Received_from_SWIFT\Error_files\E03248681_error.out", @"C:\E03248681_error.out", true);


\\Error
"Logon failure: user not allowed to log on to this computer."

按要求完成代码

 [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool LogonUser(
        string lpszUsername,
        string lpszDomain,
        string lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        out IntPtr phToken
        );

IntPtr userHandle = IntPtr.Zero;
bool loggedOn = LogonUser(userid, domain, pass, 9, 0, out userHandle);

 if (loggedOn)
 {
    WindowsImpersonationContext context = WindowsIdentity.Impersonate(userHandle);
           File.Copy(@"\\sins00048178\D$\BNCustody\Swift\Received_from_SWIFT\Error_files\E03248681_error.out", @"C:\E03248681_error.out", true);

    context.Undo();

 }

提前谢谢....

【问题讨论】:

  • 请向我们展示您的 dll 导入和实际调用。
  • 您是否使用 runas 命令测试过凭据?
  • @Denish 这部分属于哪种应用程序?一些 Windows 服务/IIS?
  • 我想用窗口应用来做。
  • @Denish 你在 Windows 2000 下运行吗?

标签: c# impersonation


【解决方案1】:

我的模拟代码类似,但与您的代码有细微差别。这是从我小组的其他开发人员那里传下来的,我确信它是从网上某处复制/粘贴的。但它确实有效,我们在 Windows 服务和表单中使用它。

//defined elsewhere
WindowsImpersonationContext impersonatedUser;
WindowsIdentity newId;
IntPtr tokenHandle;

//Impersonate
tokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(userName, domainName, password, 2, 0, ref tokenHandle);
if (returnValue) {
    newId = new WindowsIdentity(tokenHandle);
    impersonatedUser = newId.Impersonate();
} else {
    //do some error handling
}

//Undo impersonation
if (impersonatedUser != null) {
    impersonatedUser.Undo();
}
if (tokenHandle != IntPtr.Zero) {
    CloseHandle(tokenHandle);
}

【讨论】:

    猜你喜欢
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    相关资源
    最近更新 更多