"指定的登录会话不存在。它可能已经被终止了。”尝试使用 WindowsIdentity.Impersonate 复制文件时
我正在尝试将文件从共享点复制到 unc 路径。我使用以下代码:
var id = new WindowsIdentity("[email protected]");
var p = new WindowsPrincipal(id);
var wic = id.Impersonate();
File.Move(oldName, newName);
wic.Undo();
oldname is C:\test.txt newName 是 \\server\folder\test.txt
我收到错误
A specified logon session does not exist. It may already have been terminated.
如何删除此错误或使用共享点将文件从 A 复制到 B(UNC 路径)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下类将允许您通过用户名和密码访问 SMB 共享。在 Windows 到 Windows 和 Windows 到 UNIX 环境中将文件复制到网络共享时,我一直使用它。这样做的好处是您不需要进行用户身份验证。本质上,您可以使用仅目标计算机本地的凭据。
The following class will allow you to access SMB shares via a user-name and password. I use it all the time when copying files to network shares in both windows-to-windows and windows-to-unix environments. The benefit here is that you do not need to be authenticate as the user. In essence, you can use credentials that are local only to the target machine.
您实际上并没有获得要模拟的登录令牌。您需要调用 LogonUser API 来获取有效的登录令牌,您可以使用该令牌来模拟该用户。
请参阅此博客条目 例如,C# 中模拟 Windows 用户的代码。您还可以查看这篇知识库文章,其中还包含 C# 代码(以及针对以下问题的重复令牌调用) .net 1.0)
You are not actually getting a logon token to impersonate. You need to call the LogonUser API to get a valid logon token, which you can use to impersonate that user.
See this blog entry for example code in c# on impersonating a windows user. You can also see this KB article that also contains c# code (and a duplicatetoken call for issues with .net 1.0)