2026年1月22日 星期四

[研究]GCB,IIS,ASP.NET設定建議,.NET信任層級(trust leve)實測(一)

[研究]GCB,IIS,ASP.NET設定建議,.NET信任層級(trust leve)實測(一)

2025-01-21

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 20.2

********************************************************************************

發現ASP.NET WebForm 預設建立的方案,使用 .NET Framework 4.8


程式只有顯示 test 字串,沒別的。

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
    Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        test
    </form>
</body>
</html>

Default.aspx.cs

using System;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

********************************************************************************

Deploy 到網站後,先測試網站正常


下圖,把預設值 Full 改為 High

下圖,測試

錯誤訊息

'/' 應用程式中發生伺服器錯誤。
安全性例外狀況
描述: 應用程式嘗試執行安全原則不允許的作業。如果要授與這個應用程式所需的權限,請聯繫您的系統管理員,或在組態檔中變更這個應用程式的信任層級。

例外狀況詳細資訊: System.Security.SecurityException: 要求已經失敗。

原始程式錯誤:

在執行目前 Web 要求期間,產生未處理的例外狀況。如需有關例外狀況來源與位置的資訊,可以使用下列的例外狀況堆疊追蹤取得。

堆疊追蹤:


[SecurityException: 要求已經失敗。]
   System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type) +0
   System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName) +96
   System.Type.GetType(String typeName) +62
   System.CodeDom.Compiler.CompilerInfo.get_IsCodeDomProviderTypeValid() +15
   System.Web.Compilation.CompilationUtil.GetRecompilationHash(CompilationSection ps) +2406
   System.Web.Configuration.CompilationSection.get_RecompilationHash() +110
   System.Web.Compilation.BuildManager.CheckTopLevelFilesUpToDateInternal(Int64 cachedHash) +551
   System.Web.Compilation.BuildManager.CheckTopLevelFilesUpToDate(Int64 cachedHash) +59
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +155
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +746

版本資訊: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.8.4797.0

********************************************************************************

ASP.NET WebForm,System.Security.Permissions.ReflectionPermission 是甚麼 ?

System.Security.Permissions.ReflectionPermission 不是第三方套件,而是:

  • 👉 .NET Framework 內建的類別
  • 👉 隸屬於 程式碼存取安全性(Code Access Security, CAS) 架構

📦 所屬 Assembly(組件):mscorlib.dll

也就是說:

  • 不需要安裝 NuGet
  • 不需要額外引用 DLL
  • 只要是 .NET Framework(非 .NET Core / .NET 5+) 都一定存在

********************************************************************************
CAS被微軟棄用,建議設定Full,否則很多程式庫不能用。但微軟對 .NET 4.0 開始宣導使用 Full Trust。

Code Access Security
Because Code Access Security is deprecated, most modern code within the .NET ecosystem is not designed to operate within a partial trust environment. Applications that rely on CAS should not expect modern libraries to behave correctly within these environments, unless the library developer has taken explicit steps to ensure compatibility within the CAS sandbox.
由於程式碼存取安全性 (CAS) 已被棄用,.NET 生態系統中的大多數現代程式碼並非設計用於在部分信任環境中運作。依賴 CAS 的應用程式不應期望現代函式庫在這些環境中正常運行,除非函式庫開發者已採取明確措施確保其與 CAS 沙箱的兼容性。

********************************************************************************


看來以 ASP.NET WebForm 專案而言,其使用的 .NET Framerwork 的 .NET信任層級(trust leve) 必須 Full Trust。(除非你在 IIS 上不要架設 ASP.NET 網站,例如改用 PHP 或其他)

(完)

相關

.NET Framework 生命週期
https://learn.microsoft.com/zh-tw/lifecycle/products/microsoft-net-framework

沒有留言:

張貼留言