ASP.Net使用ADSI操作IIS遇到的0x80070005等问题研究记录
想通過使用ADSI對(duì)IIS的進(jìn)程池進(jìn)行控制,如重啟等操作
先用C#寫了控制臺(tái)程序,測試沒問題后,改成aspx的Handler程序,結(jié)果發(fā)布到IIS報(bào)錯(cuò):
拒絕訪問。 (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
查找解決方法:
制面板-》管理工具-》組件服務(wù)-》計(jì)算機(jī)-》我的電腦-》DCom配置-》找到IIS Admin Service
單擊屬性,"安全"選項(xiàng)卡,
分別在"啟動(dòng)和激活權(quán)限"和"訪問權(quán)限"組中選中"自定義"
編輯->添加ASP.NET賬戶和IUSER_計(jì)算機(jī)名
按照上面打開屬性框后發(fā)現(xiàn)都是灰色不可更改,解決方案:
在DCom屬性-常規(guī)中可以看到組件的“應(yīng)用程序ID{一大串?dāng)?shù)字字母組合}”,記住這個(gè)ID
regedit打開注冊表,HKEY_CLASSES_ROOT/APPID下找上面ID
在左側(cè)樹上——右鍵——權(quán)限——高級(jí)——所有者
把所有者改為administrators組,確定,關(guān)閉
重新打開組件服務(wù),IIS Admin Service的屬性就可以編輯了
編輯之后發(fā)現(xiàn)頁面報(bào)錯(cuò)問題并沒有解決,于是嘗試了下面的方法,可以正常執(zhí)行,
但是在web.config中使用身份模擬,使用明文配置,這種方法不太安全
< system.web >?
< identity impersonate =”true” userName =”你的用戶名” password =”密碼” />
</system.web>
另外還可以代碼中實(shí)現(xiàn)身份模擬,比配置中好一些,也不是我想要的方案
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())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //要執(zhí)行的代碼
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
最后一種方案比較可行,也比較簡單:
IIS-應(yīng)用程序池-高級(jí)設(shè)置,把進(jìn)程模型-標(biāo)識(shí)(identity)改為LocalSystem
另外關(guān)于 標(biāo)識(shí) ApplicationPoolIdentity
這是從IIS 7.5之后新添加的Built-in account,是IIS創(chuàng)建新application pool時(shí)默認(rèn)選擇的運(yùn)行帳號(hào)。該帳號(hào)在啟動(dòng)Application Pool是啟動(dòng)一個(gè)虛擬帳號(hào),虛擬帳號(hào)名與Application Pool同名,在用戶管理中找不到虛擬帳號(hào),但在Task Manager中可以看到w3wp.exe運(yùn)行在該虛擬帳號(hào)下。最后如果想給該虛擬帳號(hào)賦予權(quán)限,需要賦給IIS AppPool\AppPoolName
?
附另一篇供參考 ? ?
DirectoryEntry配置IIS7出現(xiàn)ADSI Error:未知錯(cuò)誤(0x80005000)
出處 https://cloud.tencent.com/developer/article/1051595
一、錯(cuò)誤情況
環(huán)境:win7+iis7.0
DirectoryEntry配置IIS7出現(xiàn)如下錯(cuò)誤
或者是
下面一段代碼在IIS6.0下運(yùn)轉(zhuǎn)正常,但I(xiàn)IS7.0下運(yùn)轉(zhuǎn)會(huì)出錯(cuò):
System.DirectoryServices.DirectoryEntry iisServer;
iisServer = new System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/1");
System.DirectoryServices.DirectoryEntry rootFolder = iisServer.Children.Find("Root","IIsWebVirtualDir");//此處拋出異常
異常內(nèi)容如下:?
[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)?
二、錯(cuò)誤分析
這段異常代碼表明 IIS://localhost/W3SVC/1 的ADSI provider不存在或者無法訪問。
打開IIS管理器你可以看到服務(wù)器的localhost(即默認(rèn)站點(diǎn))是存在的并正在運(yùn)行,且主站點(diǎn)ID確實(shí)是1。這說明問題是出現(xiàn)在 IIS://localhost的ADSI provider。
三、錯(cuò)誤原因
win7使用的是iis7,而IIS 7默認(rèn)并沒有安裝ADSI provider。?
四、解決方法?
要解決這個(gè)問題就得安裝“IIS 元數(shù)據(jù)庫和IIS 6配置兼容性”。
“控制面板”->“程序和功能”->面板左側(cè)“打開或關(guān)閉windows功能”->“Internet信息服務(wù)”->“Web管理工具”->“IIS 6管理兼容性”->“IIS 元數(shù)據(jù)庫和IIS 6配置兼容性”。
如下圖所示:
五、更好的解決方法
更理想的解決方式是用 WMI provider操作IIS 7 ,可參見此篇文章http://msdn.microsoft.com/en-us/library/aa347459.aspx
六、Windows Server 2008出現(xiàn)這種錯(cuò)誤怎么辦?
在Windows Server 2008下,使用角色服務(wù)安裝完“IIS 元數(shù)據(jù)庫和IIS 6配置兼容性”,還有可能出現(xiàn)如下錯(cuò)誤: ?
[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)
這是因?yàn)?Windows Server 2008被UAC(User Account Control,用戶賬戶控制)鎖定了. ?你需要用管理員(Administrator)賬戶執(zhí)行這個(gè)程序. 另一種方式是設(shè)置運(yùn)行此程序的賬戶擁有如下權(quán)限: Logon as a Service"/ "Logon as a Batch Job"
總結(jié)
以上是生活随笔為你收集整理的ASP.Net使用ADSI操作IIS遇到的0x80070005等问题研究记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js爬虫
- 下一篇: 设计模式简介 - Introductio