【发布时间】:2010-03-23 15:40:22
【问题描述】:
背景:我有一个应用程序必须读取网络驱动器 (Z:) 上的文件
这在我的办公室域中效果很好,但在现场(在不同的域中)不起作用。据我所知,域用户和网络驱动器的设置方式相同,但是我无权访问客户域中的用户等。
当我无法访问网络驱动器时,我想我需要一个用户令牌。这就是我模仿用户的方式:
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
...
const string userName = "USER";
const string pass = "PASS";
const string domainName = "VALIDDOMAIN.local" //tried with valid domain name and with null, same result
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
IntPtr tokenHandle = new IntPtr(0);
bool returnValue = LogonUser(userName, domainName, pass,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if (!returnValue)
throw new Exception("Logon failed.");
WindowsImpersonationContext impersonatedUser = null;
try
{
WindowsIdentity wid = new WindowsIdentity(tokenHandle);
impersonatedUser = wid.Impersonate();
}
finally
{
if (impersonatedUser != null) impersonatedUser.Undo();
}
现在是有趣/奇怪的部分。在我的网络中,应用程序已经可以访问网络驱动器,如果我尝试模拟活动用户(完全相同的用户,包括相同的域),它将无法访问网络驱动器。
这让我很无助,因为现在我不知道什么有效,什么无效,更重要的是,它会在现场有效吗?
我错过了什么?
编辑:我在最初问这个问题时忘记写这个:我尝试输入一个有效的域名但它不起作用,所以之后我尝试输入 null 以获得相同的用户名就像我没有这个代码一样(因为它在我们的域中默认工作)。这没有帮助,这就是 domain = null;结束了这个问题。
【问题讨论】:
-
我敢打赌超级用户会反对(因为这有代码)。
-
为什么不授予执行此代码的帐户对驱动器的读取权限?然后你的代码变成这样:
File.ReadAllBytes(filePath); -
我曾经问过类似的问题;您可能会发现它很有用:stackoverflow.com/questions/659013/…
-
@Codesleuth:我发表评论时它没有代码。检查时间戳。
标签: c# network-drive