【发布时间】:2014-03-06 15:49:18
【问题描述】:
用户需要在我们的应用程序启动时使用他的 Windows 凭据登录。这些凭据用于模拟用户并在提供的登录名下运行主表单。我们现在有一个OpenFileDialog,用户可以在其中选择文件。
现在当用户访问映射的网络驱动器时出现问题(这些驱动器是从用户登录到机器而不是我的程序显示的)。当按下 OK 按钮时,OpenFileDialog 会显示一条错误消息(路径无法找到/访问。请确保它存在)。
正如我在其他帖子中看到的,可以将这些路径映射回 UNC 路径,但对话框甚至没有返回,所以我可以这样做。除了制作我自己的打开文件对话框之外,还有其他解决方法吗?
模仿部分:
bool success = NativeMethods.LogonUser(userName, domain, password, (int)LogonType.Logon32LogonNewCredentials, (int)LogonProvider.Logon32ProviderWinnt50, ref pExistingTokenHandle);
if (success)
{
success = NativeMethods.DuplicateToken(pExistingTokenHandle, (int)SecurityImpersonationLevel.SecurityImpersonation, ref pDuplicateTokenHandle);
if (success)
{
// Return the impersonation context
WindowsIdentity identity = new WindowsIdentity(pDuplicateTokenHandle);
impersonationContext = identity.Impersonate();
return impersonationContext;
}
}
打开对话框部分
OpenFileDialog openFileDialog = new OpenFileDialog
{
Multiselect = true,
InitialDirectory = Environment.CurrentDirectory,
Title = "Select file"
};
bool? dialogResult = openFileDialog.ShowDialog(this);
if (dialogResult.Value)
{
openFileDialog.FileNames.ToList().ForEach(t => MessageBox.Show("File: " + t));
}
【问题讨论】:
-
您尝试过以提升的权限运行您的应用程序吗?
-
是的,但这不会给我驱动器映射
标签: c# wpf impersonation openfiledialog