ASP.Net使用ADSI操作IIS遇到的0x80070005等问题研究记录

想通过使用ADSI对IIS的进程池进行控制,如重启等操作
先用C#写了控制台程序,测试没问题后,改成aspx的Handler程序,结果发布到IIS报错:
拒绝访问。 (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

查找解决方法:

制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到IIS Admin Service
单击属性,"安全"选项卡,
分别在"启动和激活权限"和"访问权限"组中选中"自定义"
编辑->添加ASP.NET账户和IUSER_计算机名

按照上面打开属性框后发现都是灰色不可更改,解决方案:

在DCom属性-常规中可以看到组件的“应用程序ID{一大串数字字母组合}”,记住这个ID
regedit打开注册表,HKEY_CLASSES_ROOT/APPID下找上面ID
在左侧树上——右键——权限——高级——所有者
把所有者改为administrators组,确定,关闭
重新打开组件服务,IIS Admin Service的属性就可以编辑了

编辑之后发现页面报错问题并没有解决,于是尝试了下面的方法,可以正常执行,
但是在web.config中使用身份模拟,使用明文配置,这种方法不太安全
< system.web > 
< identity impersonate =”true” userName =”你的用户名” password =”密码” />
</system.web>

另外还可以代码中实现身份模拟,比配置中好一些,也不是我想要的方案
sing System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;

private const int LOGON_TYPE_INTERACTIVE = 2;
private const int LOGON_TYPE_PROVIDER_DEFAULT = 0;
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static public extern bool LogonUser(string userName, string domain, string password, int logonType, int logonProvider, ref IntPtr accessToken); 
 
public ExcelExport(string tableName)
{
    IntPtr accessToken = IntPtr.Zero;
    if (LogonUser("administrator", "domain", "password", LOGON_TYPE_INTERACTIVE, LOGON_TYPE_PROVIDER_DEFAULT, ref accessToken))
    {
 
        using (WindowsIdentity identity = new WindowsIdentity(accessToken))
        {
            using (WindowsImpersonationContext context = identity.Impersonate())
            {
                //要执行的代码
            }
        }
    }
}

最后一种方案比较可行,也比较简单:

IIS-应用程序池-高级设置,把进程模型-标识(identity)改为LocalSystem

另外关于 标识 ApplicationPoolIdentity

这是从IIS 7.5之后新添加的Built-in account,是IIS创建新application pool时默认选择的运行帐号。该帐号在启动Application Pool是启动一个虚拟帐号,虚拟帐号名与Application Pool同名,在用户管理中找不到虚拟帐号,但在Task Manager中可以看到w3wp.exe运行在该虚拟帐号下。最后如果想给该虚拟帐号赋予权限,需要赋给IIS AppPool\AppPoolName
 

附另一篇供参考    
DirectoryEntry配置IIS7出现ADSI Error:未知错误(0x80005000)
出处 https://cloud.tencent.com/developer/article/1051595

一、错误情况

环境:win7+iis7.0

DirectoryEntry配置IIS7出现如下错误

或者是

下面一段代码在IIS6.0下运转正常,但IIS7.0下运转会出错:

System.DirectoryServices.DirectoryEntry iisServer;
iisServer = new System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/1");
System.DirectoryServices.DirectoryEntry rootFolder = iisServer.Children.Find("Root","IIsWebVirtualDir");//此处抛出异常

异常内容如下: 

[System.Runtime.InteropServices.COMException] {"Unknown error (0x80005000)"} 
System.Runtime.InteropServices.COMException

Unknown error (0x80005000)
   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_IsContainer()
   at System.DirectoryServices.DirectoryEntries.CheckIsContainer()
   at System.DirectoryServices.DirectoryEntries.Find(String name, String schemaClassName) 

二、错误分析

这段异常代码表明 IIS://localhost/W3SVC/1 的ADSI provider不存在或者无法访问。

打开IIS管理器你可以看到服务器的localhost(即默认站点)是存在的并正在运行,且主站点ID确实是1。这说明问题是出现在 IIS://localhost的ADSI provider。

三、错误原因

win7使用的是iis7,而IIS 7默认并没有安装ADSI provider。 

四、解决方法 

要解决这个问题就得安装“IIS 元数据库和IIS 6配置兼容性”。

“控制面板”->“程序和功能”->面板左侧“打开或关闭windows功能”->“Internet信息服务”->“Web管理工具”->“IIS 6管理兼容性”->“IIS 元数据库和IIS 6配置兼容性”。

如下图所示:

五、更好的解决方法

更理想的解决方式是用 WMI provider操作IIS 7 ,可参见此篇文章http://msdn.microsoft.com/en-us/library/aa347459.aspx

六、Windows Server 2008出现这种错误怎么办?

在Windows Server 2008下,使用角色服务安装完“IIS 元数据库和IIS 6配置兼容性”,还有可能出现如下错误:  

[System.Runtime.InteropServices.COMException] {"Access is denied.\r\n"} System.Runtime.InteropServices.COMException
ErrorCode 0x80070005

Access is denied.

   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_IsContainer()
   at System.DirectoryServices.DirectoryEntries.CheckIsContainer()
   at System.DirectoryServices.DirectoryEntries.Find(String name, String schemaClassName)

这是因为 Windows Server 2008被UAC(User Account Control,用户账户控制)锁定了.  你需要用管理员(Administrator)账户执行这个程序. 另一种方式是设置运行此程序的账户拥有如下权限: Logon as a Service"/ "Logon as a Batch Job"

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值