首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >冒充不为DirectoryServices工作

冒充不为DirectoryServices工作
EN

Stack Overflow用户
提问于 2012-03-08 15:51:31
回答 2查看 2.4K关注 0票数 1

我正在尝试执行以下代码

代码语言:javascript
复制
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;
    }
}

由于我需要服务器上的管理员权限来执行这段代码,所以我使用这个类模拟正确的用户:

代码语言:javascript
复制
using (Impersonator impersonator = new Impersonator("username", "domain", "password"))
{
    server.HasAccess = HasVirtualDirectory(server.HostName, virtualDirectory);
}

但是我仍然得到了COMException:访问被拒绝。另一方面,如果我不使用模拟程序,但直接使用在模拟中使用的凭据运行程序(通过在上下文菜单中使用“Runas不同凡响用户”),它就会像预期的那样工作。

以管理员身份运行程序(运行程序的计算机上的管理员,而不是服务器上的管理员)没有更改任何内容,异常仍然发生。

我还在ImpersonationLevel.SecurityDelegation调用中尝试了ImpersonationLevel.SecurityImpersonation (=3)而不是ImpersonationLevel.SecurityImpersonation (=2),但这并没有改变任何东西(无论是作为正常用户还是作为执行程序的管理员用户)。

为了测试模拟代码,我尝试了下面的代码,这是有效的。(执行程序的用户没有创建目录的权限,但模拟用户拥有)。

代码语言:javascript
复制
using (Impersonator impersonator = new Impersonator("username", "domain", "password"))
{
    Directory.CreateDirectory(@"\\servername\c$\tmp");
}

我使用Windows 7专业和UAC激活。服务器是Windows 2003 R2 SP2。

有人有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-08 19:42:18

使用使用用户名和密码而不是模拟的DirectoryEntry构造器(String,AuthenticationTypes)

代码语言:javascript
复制
DirectoryEntry directoryEntry = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/Root", @"domain\username", "password", AuthenticationTypes.Secure | AuthenticationTypes.Sealing);
票数 2
EN

Stack Overflow用户

发布于 2012-03-08 16:47:33

假设您使用的是来自CodeProject的模拟器类,请尝试更改登录类型,如下文中注释的第4页所示:

嗨,乌维, 只有当您将logonuser函数中的logonuser函数中的logontype更改为LOGON32_LOGON_NEW_CREDENTIALS时,它才能从vista进行远程访问。 const LOGON32_LOGON_NEW_CREDENTIALS = 9; 请参阅LogonUser函数 问候Uwe

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9620566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档