2026年1月15日 星期四

[研究]GCB IIS 10.0 基本設定,「匿名使用者識別」從「IUSR」改為「應用程式集區識別」實測

[研究]GCB IIS 10.0 基本設定,「匿名使用者識別」從「IUSR」改為「應用程式集區識別」實測

2026-01-15

政府組態基準(Government Configuration Baseline,簡稱GCB)

[研究]Windows Server 2019 套用 GCB IIS 10.0設定路徑圖解(1)基本設定
https://shaurong.blogspot.com/2025/10/windows-server-2019-gcb-iis-1001.html

[研究]ASP.NET WebForm 用 NuGet 安裝 Microsoft Identity 身分識別機制套件https://shaurong.blogspot.com/2026/01/aspnet-webform-nuget-microsoft-identity.html

項次TWGCB-ID類別原則設定名稱說明設定位置設定路徑GCB設定值
6TWGCB-04-014-0006基本設定匿名使用者識別
  • 這項原則設定決定匿名使用者識別是否設為「應用程式集區識別」
  • 將匿名驗證中之「匿名使用者識別」設定為「應用程式集區識別」,可確保匿名使用者以最小權限之身分執行,可簡化站台管理工作
伺服器IIS 管理員\伺服器\IIS\驗證\動作\開啟功能\匿名驗證\動作\編輯\編輯匿名驗證認證\匿名使用者識別應用程式集區識別




註:如果編輯「匿名驗證」出現錯誤,請檢查該網站是否存在

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

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

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

Default.aspx

<?xml version="1.0" encoding="utf-8"?>
<!--
  如需如何設定 ASP.NET 應用程式的詳細資訊,請前往
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.8" />
    <httpRuntime targetFramework="4.8" />
    <authentication mode="Forms" />
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>

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">
        <h2>IIS GCB 匿名驗證測試</h2>

        <table border="1">
            <tr>
                <th>項目</th>
                <th>值</th>
            </tr>
            <tr>
                <td>User.Identity.IsAuthenticated</td>
                <td><asp:Label ID="lblIsAuthenticated" runat="server" /></td>
            </tr>
            <tr>
                <td>User.Identity.Name</td>
                <td><asp:Label ID="lblUserName" runat="server" /></td>
            </tr>
            <tr>
                <td>HttpContext.Current.User.Identity.AuthenticationType</td>
                <td><asp:Label ID="lblAuthType" runat="server" /></td>
            </tr>
            <tr>
                <td>Request.LogonUserIdentity.Name</td>
                <td><asp:Label ID="lblLogonUser" runat="server" /></td>
            </tr>
        </table>

        <div class="section">
        <h3>Forms Authentication 測試登入</h3>
        帳號:
        <asp:TextBox ID="txtUser" runat="server" />
        <asp:Button ID="btnLogin" runat="server" Text="登入" OnClick="btnLogin_Click" />
        &nbsp;
        <asp:Button ID="btnLogout" runat="server" Text="登出" OnClick="btnLogout_Click" />
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Web.Security;

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

        private void ShowIdentity()
        {
            lblIsAuthenticated.Text = User.Identity.IsAuthenticated.ToString();

            lblUserName.Text = string.IsNullOrEmpty(User.Identity.Name)
                ? "(空字串)"
                : User.Identity.Name;

            lblAuthType.Text = string.IsNullOrEmpty(User.Identity.AuthenticationType)
                ? "(null / 空)"
                : User.Identity.AuthenticationType;

            if (Request.LogonUserIdentity != null)
            {
                lblLogonUser.Text = Request.LogonUserIdentity.Name;
            }
            else
            {
                lblLogonUser.Text = "(null)";
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            // 不做帳密驗證,純測試用
            string userName = txtUser.Text.Trim();

            if (!string.IsNullOrEmpty(userName))
            {
                FormsAuthentication.SetAuthCookie(userName, false);
                Response.Redirect(Request.RawUrl);
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Response.Redirect(Request.RawUrl);
        }
    }
}

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

下圖,在 Visual Studio Enterprise 2019 中執行,登入前


下圖,在 Visual Studio Enterprise 2019 中執行,登入後


下圖,未套 GCB,Deploy後,登入前


下圖,未套 GCB,Deploy後,登入後


下圖,套 GCB,Deploy後,登入前

下圖,套 GCB,Deploy後,登入後

Request.LogonUserIdentity.Name 在套用 GCB 前後回傳值可能有變化。

[研究]GCB IIS 10.0「匿名使用者識別」從「IUSR」改為「應用程式集區識別」會否產生問題?

[研究]GCB IIS 10.0「匿名使用者識別」從「IUSR」改為「應用程式集區識別」會否產生問題?(二)

根據這幾篇,影響的東西會很多,有空再繼續研究。

(完)

相關



沒有留言:

張貼留言