我正在尝试执行以下代码
using System.DirectoryServices;
public bool HasVirtualDirectory(string serverName, string virtualDirectoryName)
{
try
{
DirectoryEntry directoryEntry = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/Root");
return directoryEntry.Children.Find(virtualDirectoryName, directoryEntry.SchemaClassName.ToString()) != null;
}
catch (Exception)
{
return false;
}
}由于我需要服务器上的管理员权限来执行这段代码,所以我使用这个类模拟正确的用户:
using (Impersonator impersonator = new Impersonator("username", "domain", "password"))
{
server.HasAccess = HasVirtualDirectory(server.HostName, virtualDirectory);
}但是我仍然得到了COMException:访问被拒绝。另一方面,如果我不使用模拟程序,但直接使用在模拟中使用的凭据运行程序(通过在上下文菜单中使用“Runas不同凡响用户”),它就会像预期的那样工作。
以管理员身份运行程序(运行程序的计算机上的管理员,而不是服务器上的管理员)没有更改任何内容,异常仍然发生。
我还在ImpersonationLevel.SecurityDelegation调用中尝试了ImpersonationLevel.SecurityImpersonation (=3)而不是ImpersonationLevel.SecurityImpersonation (=2),但这并没有改变任何东西(无论是作为正常用户还是作为执行程序的管理员用户)。
为了测试模拟代码,我尝试了下面的代码,这是有效的。(执行程序的用户没有创建目录的权限,但模拟用户拥有)。
using (Impersonator impersonator = new Impersonator("username", "domain", "password"))
{
Directory.CreateDirectory(@"\\servername\c$\tmp");
}我使用Windows 7专业和UAC激活。服务器是Windows 2003 R2 SP2。
有人有什么想法吗?
发布于 2012-03-08 19:42:18
使用使用用户名和密码而不是模拟的DirectoryEntry构造器(String,AuthenticationTypes)。
DirectoryEntry directoryEntry = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/Root", @"domain\username", "password", AuthenticationTypes.Secure | AuthenticationTypes.Sealing);发布于 2012-03-08 16:47:33
假设您使用的是来自CodeProject的模拟器类,请尝试更改登录类型,如下文中注释的第4页所示:
嗨,乌维, 只有当您将logonuser函数中的logonuser函数中的logontype更改为LOGON32_LOGON_NEW_CREDENTIALS时,它才能从vista进行远程访问。 const LOGON32_LOGON_NEW_CREDENTIALS = 9; 请参阅LogonUser函数 问候Uwe
https://stackoverflow.com/questions/9620566
复制相似问题