所以我在运行Windows Server 2003 & IIS 6的生产服务器上遇到了一个代码问题。
我试图冒充一个域帐户,在本地工作正常。
但是当它在服务器上时,函数impersonate user失败了,即返回false。
private bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, 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;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}
注意,代码没有崩溃,只是返回false. 有谁以前遇到过这种情况,有什么想法,我应该从哪些方面入手?我假设IIS配置在这里起作用,但它可能需要几个星期才能找到一个小问题导致这一点。
我可以使用我试图冒充的帐户在服务器上挂载驱动器,所以帐户userpassword组合是好的,可以用来在Windows资源管理器中进行验证。
我记得,我有同样的问题,在一个项目中,冒充不工作的综合模式,该问题的解决方案是添加到我们的web.config。
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
但这样一来,冒名顶替就不能在 global.asax 方法中工作了。
protected void Application_AuthenticateRequest(object sender, EventArgs e)
protected void Application_BeginRequest(object sender, EventArgs e)
希望这也能帮助你,因为我不知道这是哪个ISS版本的。