基本上我遇到了与这篇文章在 ASP.NET 中模拟时访问映射驱动器相同的问题
我正在开发一个旧网站,我需要允许管理员将网站的徽标、横幅等从他们桌面上的图像文件更改为服务器上的映射驱动器。
因此,他们的网站在需要保存驱动器时使用模拟,并且运行良好;但是我无法让它在他们的测试环境和我的测试环境中工作。
有任何想法吗?我已经仔细检查了用户和密码(代码没有指定域),这不是问题。
以下是处理模拟的代码摘录:
public bool ImpersonateUser(String user, String password, String domain)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(user, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
//... rest of the code
还有一个-sanitized-测试:
if (impUtility.ImpersonateUser("user", "password", string.Empty))
{
fu.SaveAs(@"C:\Images\" + imgName);
}