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"
總結
以上是生活随笔為你收集整理的ASP.Net使用ADSI操作IIS遇到的0x80070005等问题研究记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js爬虫
- 下一篇: 设计模式简介 - Introductio