[Bug 逃走中][ASP.NET] ASP.NET 無法存取 Temporary ASP.NET Files 的問題

Bug 逃走中,我們將扮演獵人,追捕躲在程式角落或是各種不同問題內的 bug: ASP.NET 無法存取 Temporary ASP.NET Files 的問題

Bug 逃走中,我們將扮演獵人,追捕躲在程式角落或是各種不同問題內的 bug (不論是程式中的 bug 或是無法讓程式正常運作的,都是 bug)~

 

平常在開發 ASP.NET 應用程式時,多半不會有人關心是誰執行 ASP.NET 應用程式,但這點在部署應用程式時卻十分重要,尤其是在有網域環境的企業內,因為有時要存取網域內的資源,或是網管不允許開發人員使用本機的帳戶,此時開發人員就要設定 IIS (在應用程式集區,IIS 目錄安全設定或是在 Web.config 中使用 Impersonate) 以使用網域帳戶,但通常設好以後會有一定的機率看到這個訊息:

Server Error in '/DirectoryUpdate' Application.
--------------------------------------------------------------------------------

The current identity (NT AUTHORITY\NETWORK SERVICE) does not have write access to 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The current identity (NT AUTHORITY\NETWORK SERVICE) does not have write access to 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[HttpException (0x80004005): The current identity (NT AUTHORITY\NETWORK SERVICE) does not have write access to 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files'.]
System.Web.HttpRuntime.SetUpCodegenDirectory(CompilationSection compilationSection) +3482395
System.Web.HttpRuntime.HostingInit(HostingEnvironmentFlags hostingFlags) +226
[HttpException (0x80004005): The current identity (NT AUTHORITY\NETWORK SERVICE) does not have write access to 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files'.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +3435023
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +88
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +252
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210

 

因為在一開始的時候,ASP.NET 是用內建帳戶 (ex: Network Services 或 ASPNET) 執行,這些帳戶都已有授權可存取位於 .NET Framework 安裝目錄下的 Temporary ASP.NET Files 目錄 (這是給 ASP.NET Runtime Engine 存放暫存檔案用的),如果改為網域帳戶的話,很可能因為忘了授權它可讀寫這個目錄,而出現上述的錯誤訊息。

解決方法很簡單,只要將這個目錄的讀寫 (read/write) 權限設定給要跑 ASP.NET 應用程式的網域帳戶即可。