在 ASP.NET Core 中設定 Windows 驗證

作者:Rick AndersonKirk Larkin

Windows 驗證 (也稱為 Negotiate、Kerberos 或 NTLM 驗證) 可以針對裝載于IISKestrelHTTP.sys的 ASP.NET Core應用程式進行設定。

Windows 驗證依賴作業系統來驗證 ASP.NET Core應用程式的使用者。 Windows 驗證用於使用 Active Directory 網域身分識別或 Windows 帳戶在公司網路上執行的伺服器,以識別使用者。 Windows 驗證最適合使用者、用戶端應用程式和網頁伺服器屬於相同 Windows 網域的內部網路環境。

注意

HTTP/2 不支援 Windows 驗證。 驗證挑戰可以在 HTTP/2 回應上傳送,但用戶端必須在驗證之前降級為 HTTP/1.1。

Proxy 和負載平衡器案例

Windows 驗證是主要用於內部網路的具狀態案例,其中 Proxy 或負載平衡器通常不會處理用戶端與伺服器之間的流量。 如果使用 Proxy 或負載平衡器,Windows 驗證僅適用于 Proxy 或負載平衡器:

  • 處理驗證。
  • 例如,在要求標頭) 中,將使用者驗證資訊傳遞至應用程式 (,這會對驗證資訊採取動作。

使用 Proxy 和負載平衡器之環境中的 Windows 驗證替代方式是 Active Directory 同盟服務 (ADFS) 與 OpenID Connect (OIDC) 。

IIS/IIS Express

在 中 Program.cs 呼叫 AddAuthentication ,以新增 NuGet 套件Microsoft.AspNetCore.Authentication.Negotiate和驗證服務:

using Microsoft.AspNetCore.Authentication.Negotiate;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
   .AddNegotiate();

builder.Services.AddAuthorization(options =>
{
    options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddRazorPages();

var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();

app.Run();

上述程式碼是由指定Windows 驗證的 Razor ASP.NET Core Pages 範本所產生。

啟動設定 (偵錯工具)

啟動設定的組態只會影響 Properties/launchSettings.json IIS Express的檔案,而且不會設定 Windows 驗證的 IIS。 IIS一節會說明伺服器組態。

您可以設定透過 Visual Studio 或 .NET Core CLI 取得的 Web 應用程式 範本,以支援 Windows 驗證,以自動更新 Properties/launchSettings.json 檔案。

新增專案

建立新的 Razor Pages 或 MVC 應用程式。 在 [ 其他資訊] 對話方塊中,將 [驗證類型 ] 設定為 [Windows]。

執行應用程式。 使用者名稱會出現在轉譯應用程式的使用者介面中。

現有專案

專案的屬性會啟用 Windows 驗證,並停用匿名驗證。 開啟 [啟動設定檔] 對話方塊:

  1. 在 [方案總管] 中,以滑鼠右鍵按一下專案,然後選取 [屬性]。
  2. 選取 [偵錯] > [一般] 索引標籤,然後選取 [開啟偵錯啟動設定檔 UI]。
  3. 清除 [啟用匿名驗證] 核取方塊。
  4. 選取 [啟用 Windows 驗證]核取方塊。

或者,您可以在檔案的 launchSettings.json 節點中 iisSettings 設定屬性:

"iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
        "applicationUrl": "http://localhost:52171/",
        "sslPort": 44308
    }
}

IIS

IIS 會使用ASP.NET Core 模組來裝載 ASP.NET Core應用程式。 Windows 驗證是透過 web.config 檔案來設定 IIS。 下列各節將示範如何:

  • 提供本機 web.config 檔案,以在部署應用程式時啟用伺服器上的 Windows 驗證。
  • 使用 IIS 管理員來設定已部署至伺服器之 ASP.NET Core應用程式的web.config檔案。

如果您尚未這麼做,請啟用 IIS 來裝載 ASP.NET Core應用程式。 如需詳細資訊,請參閱在使用 IIS 的 Windows 上裝載 ASP.NET Core

啟用 Windows 驗證的 IIS 角色服務。 如需詳細資訊,請參閱 在 IIS 角色服務中啟用 Windows 驗證 (請參閱步驟 2)

IIS 整合中介軟體 預設會設定為自動驗證要求。 如需詳細資訊,請參閱使用 IIS 在 Windows 上裝載 ASP.NET Core:IIS 選項 (AutomaticAuthentication)

ASP.NET Core模組預設會設定為將 Windows 驗證權杖轉送至應用程式。 如需詳細資訊,請參閱ASP.NET Core模組組態參考:aspNetCore 元素的屬性

使用下列 其中一 種方法:

  • 發佈和部署專案之前, 請將下列 web.config 檔案新增至專案根目錄:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <location path="." inheritInChildApplications="false">
        <system.webServer>
          <security>
            <authentication>
              <anonymousAuthentication enabled="false" />
              <windowsAuthentication enabled="true" />
            </authentication>
          </security>
        </system.webServer>
      </location>
    </configuration>
    

    當 .NET Core SDK 發行專案時, (專案檔中未 <IsTransformWebConfigDisabled> 將 屬性設定 true 為) ,則已發佈 的web.config 檔案會包含 <location><system.webServer><security><authentication> 區段。 如需 屬性的詳細資訊 <IsTransformWebConfigDisabled> ,請參閱使用 IIS 在 Windows 上裝載 ASP.NET Core

  • 發佈和部署專案之後, 請使用 IIS 管理員執行伺服器端設定:

    1. 在 [IIS 管理員] 中,選取 [連線] 提要欄位之 [月臺] 節點底下的 IIS台。
    2. 按兩下IIS區域中的 [驗證]。
    3. 選取 [匿名驗證]。 在[動作]提要欄位中選取 [停用]。
    4. 選取 [Windows 驗證] 。 在[動作]提要欄位中選取 [啟用]。

    當採取這些動作時,IIS 管理員會修改應用程式的 web.config 檔案。 <system.webServer><security><authentication>已針對 和 windowsAuthentication 新增更新設定 anonymousAuthentication 的節點:

    <system.webServer>
      <security>
        <authentication>
          <anonymousAuthentication enabled="false" />
          <windowsAuthentication enabled="true" />
        </authentication>
      </security>
    </system.webServer>
    

    <system.webServer>IIS 管理員新增至web.config檔案的區段不在發佈應用程式 <location> 時由 .NET Core SDK 新增的應用程式區段之外。 因為 區段是在節點外部 <location> 新增,所以任何 子應用程式 都會繼承設定至目前的應用程式。 若要防止繼承,請在 .NET Core SDK 提供的 區段中移動新增 <security> 的 區段 <location><system.webServer>

    當 IIS 管理員用來新增 IIS 組態時,它只會影響伺服器上的應用程式 web.config 檔案。 如果伺服器的 web.config 複本已由專案的 web.config 檔案取代,則應用程式的後續部署可能會覆寫伺服器上的設定。 使用下列 任一 方法來管理設定:

    • 在部署時覆寫檔案之後,使用 IIS 管理員重設 web.config 檔案中的設定。
    • 使用設定在本機將 web.config檔案 新增至應用程式。

Kestrel

您可以使用 Microsoft.AspNetCore.Authentication.Negotiate NuGet 套件, Kestrel 在 Windows、Linux 和 macOS 上使用 Negotiate 和 Kerberos 來支援 Windows 驗證。

警告

認證可以跨連線上的要求保存。 除非 Proxy 維護 1:1 連線親和性,否則交涉驗證不得與 Proxy 搭配使用, (持續連線) Kestrel 。

注意

Negotiate 處理常式會偵測基礎伺服器是否原生支援 Windows 驗證,以及是否啟用它。 如果伺服器支援 Windows 驗證,但已停用,則會擲回錯誤,要求您啟用伺服器實作。 在伺服器中啟用 Windows 驗證時,交涉處理常式會以透明方式將驗證要求轉送至伺服器。

下列醒目提示的程式碼會啟用驗證: Program.cs

using Microsoft.AspNetCore.Authentication.Negotiate;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
   .AddNegotiate();

builder.Services.AddAuthorization(options =>
{
    options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddRazorPages();

var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();

app.Run();

上述程式碼是由指定Windows 驗證的 Razor ASP.NET Core Pages 範本所產生。 上述程式碼中會使用下列 API:

RBAC) (Kerberos 驗證和角色型存取控制

Linux 或 macOS 上的 Kerberos 驗證不會為已驗證的使用者提供任何角色資訊。 若要將角色和群組資訊新增至 Kerberos 使用者,驗證處理常式必須設定為從 LDAP 網域擷取角色。 最基本的組態只會指定要查詢的 LDAP 網域,並使用已驗證的使用者內容來查詢 LDAP 網域:

using Microsoft.AspNetCore.Authentication.Negotiate;
using System.Runtime.InteropServices;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
    .AddNegotiate(options =>
    {
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        {
            options.EnableLdap("contoso.com");
        }
    });

某些設定可能需要特定認證才能查詢 LDAP 網域。 您可以在下列醒目提示的選項中指定認證:

using Microsoft.AspNetCore.Authentication.Negotiate;
using System.Runtime.InteropServices;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
        .AddNegotiate(options =>
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                options.EnableLdap(settings =>
                {
                    settings.Domain = "contoso.com";
                    settings.MachineAccountName = "machineName";
                    settings.MachineAccountPassword =
                                      builder.Configuration["Password"];
                });
            }
        });

builder.Services.AddRazorPages();

根據預設,交涉驗證處理常式會解析巢狀網域。 在大型或複雜的 LDAP 環境中,解析巢狀網域可能會導致查閱速度緩慢或每個使用者使用大量記憶體。 您可以使用 選項停用 IgnoreNestedGroups 巢狀網域解析。

允許匿名要求。 使用ASP.NET Core授權來挑戰匿名要求進行驗證。

Windows 環境設定

Microsoft.AspNetCore.Authentication.Negotiate元件會執行使用者模式驗證。 服務主體名稱 (SPN) 必須新增至執行服務的使用者帳戶,而不是電腦帳戶。 在系統管理命令殼層中執行 setspn -S HTTP/myservername.mydomain.com myuser

Kerberos 與 NTLM

ASP.NET Core的 Negotiate 套件 Kestrel 會嘗試使用 Kerberos,這是比NTLM更安全且更具效能的驗證配置:

using Microsoft.AspNetCore.Authentication.Negotiate;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
   .AddNegotiate();

builder.Services.AddAuthorization(options =>
{
    options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddRazorPages();

var app = builder.Build();

NegotiateDefaults.AuthenticationScheme 會指定 Kerberos,因為它是預設值。

IIS、IISExpress 及 Kestrel 同時支援 Kerberos 和 NTLM

檢查 WWW-Authenticate: 使用 IIS 或 IISExpress 搭配 Fiddler 等工具的標頭會顯示 Negotiate 或 NTLM。

Kestrel 僅顯示 WWW-Authenticate: Negotiate

標頭 WWW-Authenticate: Negotiate 表示伺服器可以使用 NTLM 或 Kerberos。 KestrelNegotiate 需要標頭前置詞,它不支援直接在要求或回應驗證標頭中指定 NTLM 。 中 Kestrel 支援 NTLM,但必須以 的形式 Negotiate 傳送。

在 上 Kestrel ,若要查看是否使用 NTLM 或 Kerberos,Base64 會將標頭解碼,並顯示 NTLMHTTPHTTP 表示已使用 Kerberos。

Linux 和 macOS 環境設定

使用Windows 驗證 - Kerberos 將 Azure Data Studio 連線到您的SQL Server中,提供將 Linux 或 macOS 電腦加入 Windows 網域的指示。 指示會在網域上建立 Linux 電腦的機器帳戶。 SPN 必須新增至該電腦帳戶。

注意

遵循使用 Windows 驗證 - Kerberos 將 Azure Data Studio 連線到您的SQL Server一文中的指引時,請視需要將 取代 python-software-propertiespython3-software-properties

將 Linux 或 macOS 機器加入網域之後,必須執行額外的步驟,才能提供具有 SPN 的 keytab 檔案

  • 在網域控制站上,將新的 Web 服務 SPN 新增至電腦帳戶:
    • setspn -S HTTP/mywebservice.mydomain.com mymachine
    • setspn -S HTTP/mywebservice@MYDOMAIN.COM mymachine
  • 使用 ktpass 來產生 keytab 檔案:
    • ktpass -princ HTTP/mywebservice.mydomain.com@MYDOMAIN.COM -pass myKeyTabFilePassword -mapuser MYDOMAIN\mymachine$ -pType KRB5_NT_PRINCIPAL -out c:\temp\mymachine.HTTP.keytab -crypto AES256-SHA1
    • 某些欄位必須以大寫指定,如所述。
  • 將 keytab 檔案複製到 Linux 或 macOS 電腦。
  • 透過環境變數選取 keytab 檔案: export KRB5_KTNAME=/tmp/mymachine.HTTP.keytab
  • klist 用 以顯示目前可用的 SPN。

注意

keytab 檔案包含網域存取認證,必須據以保護。

HTTP.sys

HTTP.sys 支援使用 Negotiate、NTLM 或 Basic 驗證 的核心模式 Windows 驗證。

下列程式碼會新增驗證,並將應用程式的 Web 主機設定為搭配 Windows 驗證使用HTTP.sys:

using Microsoft.AspNetCore.Server.HttpSys;
using System.Runtime.InteropServices;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication(HttpSysDefaults.AuthenticationScheme);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
    builder.WebHost.UseHttpSys(options =>
        {
            options.Authentication.Schemes =
                AuthenticationSchemes.NTLM |
                AuthenticationSchemes.Negotiate;
            options.Authentication.AllowAnonymous = false;
        });
}

注意

HTTP.sys使用 Kerberos 驗證通訊協定委派給 核心模式 驗證。 Kerberos 和HTTP.sys不支援使用者模式驗證。 必須使用電腦帳戶來解密 Kerberos 權杖/票證,該權杖/票證取自 Active Directory,並由用戶端將其轉送至伺服器來驗證使用者。 請註冊主機的服務主體名稱 (SPN),而非應用程式的使用者。

注意

Nano Server 1709 版或更新版本不支援HTTP.sys。 若要搭配 Nano Server 使用 Windows 驗證和HTTP.sys,請使用 Server Core (microsoft/windowsservercore) 容器。 如需 Server Core 的詳細資訊,請參閱 什麼是 Windows Server 中的 Server Core 安裝選項?

授權使用者

匿名存取的組態狀態會決定在應用程式中使用 和 [AllowAnonymous] 屬性的方式 [Authorize] 。 下列兩節說明如何處理不允許和允許匿名存取的設定狀態。

不允許匿名存取

啟用 Windows 驗證並停用匿名存取時,[ [Authorize] ] (xref:Microsoft.AspNetCore.Authorization.AuthorizationAttribute) 和 [AllowAnonymous] 屬性沒有任何作用。 如果 IIS 網站設定為不允許匿名存取,要求永遠不會到達應用程式。 因此, [AllowAnonymous] 屬性不適用。

允許匿名存取

啟用 Windows 驗證和匿名存取時,請使用 [ [Authorize] ] (xref:Microsoft.AspNetCore.Authorization.AuthorizeAttribute) 和 [AllowAnonymous] 屬性。 [ [Authorize] ] (xref:Microsoft.AspNetCore.Authorization.AuthorizationAttribute) 屬性可讓您保護需要驗證之應用程式的端點。 屬性 [AllowAnonymous][Authorize] 覆寫應用程式中允許匿名存取的屬性。 如需屬性使用方式詳細資料,請參閱ASP.NET Core中的簡單授權

注意

根據預設,缺少存取頁面授權的使用者會顯示空的 HTTP 403 回應。 StatusCodePages 中介軟體可以設定為為使用者提供更好的「拒絕存取」體驗。

模擬

ASP.NET Core不會實作模擬。 應用程式會針對所有要求使用應用程式身分識別來執行,並使用應用程式集區或進程身分識別。 如果應用程式應該代表使用者執行動作,請使用Windows Identity 。在中的終端機內嵌中介軟體Startup.Configure 中執行Impersonated 或 RunImpersonatedAsync 。 在此內容中執行單一動作,然後關閉內容。

app.Run(async (context) =>
{
    try
    {
        var user = (WindowsIdentity)context.User.Identity!;

        await context.Response
            .WriteAsync($"User: {user.Name}\tState: {user.ImpersonationLevel}\n");

        await WindowsIdentity.RunImpersonatedAsync(user.AccessToken, async () =>
        {
            var impersonatedUser = WindowsIdentity.GetCurrent();
            var message =
                $"User: {impersonatedUser.Name}\t" +
                $"State: {impersonatedUser.ImpersonationLevel}";

            var bytes = Encoding.UTF8.GetBytes(message);
            await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
        });
    }
    catch (Exception e)
    {
        await context.Response.WriteAsync(e.ToString());
    }
});

雖然 Microsoft.AspNetCore.Authentication.Negotiate 套件可在 Windows、Linux 和 macOS 上啟用驗證,但 Windows 僅支援模擬。

宣告轉換

使用 IIS 裝載時, AuthenticateAsync 不會在內部呼叫 來初始化使用者。 因此,預設會在未啟動每個驗證之後,使用 IClaimsTransformation 實作來轉換宣告。 如需啟用宣告轉換的詳細資訊和程式碼範例,請參閱 跨進程與跨進程裝載之間的差異

其他資源

Windows 驗證 (也稱為 Negotiate、Kerberos 或 NTLM 驗證) 可以針對裝載于IISKestrelHTTP.sys的 ASP.NET Core應用程式進行設定。

Windows 驗證依賴作業系統來驗證 ASP.NET Core應用程式的使用者。 當您的伺服器使用 Active Directory 網域身分識別或 Windows 帳戶在公司網路上執行時,您可以使用 Windows 驗證來識別使用者。 Windows 驗證最適合使用者、用戶端應用程式和網頁伺服器屬於相同 Windows 網域的內部網路環境。

注意

HTTP/2 不支援 Windows 驗證。 驗證挑戰可以在 HTTP/2 回應上傳送,但用戶端必須在驗證之前降級為 HTTP/1.1。

Proxy 和負載平衡器案例

Windows 驗證是主要用於內部網路的具狀態案例,其中 Proxy 或負載平衡器通常不會處理用戶端與伺服器之間的流量。 如果使用 Proxy 或負載平衡器,Windows 驗證僅適用于 Proxy 或負載平衡器:

  • 處理驗證。
  • 例如,在要求標頭) 中,將使用者驗證資訊傳遞至應用程式 (,這會對驗證資訊採取動作。

使用 Proxy 和負載平衡器之環境中的 Windows 驗證替代方式是 Active Directory 同盟服務 (ADFS) 與 OpenID Connect (OIDC) 。

IIS/IIS Express

在 中叫用 AddAuthentication (Microsoft.AspNetCore.Server.IISIntegration 命名空間) Startup.ConfigureServices 來新增驗證服務:

services.AddAuthentication(IISDefaults.AuthenticationScheme);

啟動設定 (偵錯工具)

啟動設定的組態只會影響 Properties/launchSettings.json IIS Express的檔案,而且不會設定 Windows 驗證的 IIS。 IIS一節會說明伺服器組態。

您可以設定透過 Visual Studio 或 .NET Core CLI 取得的 Web 應用程式 範本,以支援 Windows 驗證,以自動更新 Properties/launchSettings.json 檔案。

新增專案

  1. 建立新專案。
  2. 選取 ASP.NET Core Web 應用程式。 選取 [下一步] 。
  3. [專案名稱 ] 欄位中提供名稱。 確認 [位置] 專案正確,或提供專案的位置。 選取 [建立]。
  4. 選取 [驗證] 底下的[變更]。
  5. 在 [ 變更驗證] 視窗中,選取 [ Windows 驗證]。 選取 [確定]。
  6. 選取 [Web 應用程式]。
  7. 選取 [建立]。

執行應用程式。 使用者名稱會出現在轉譯應用程式的使用者介面中。

現有專案

專案的屬性會啟用 Windows 驗證,並停用匿名驗證:

  1. 以滑鼠右鍵按一下方案總管中的專案,然後選取 [屬性]。
  2. 選取 [偵錯] 索引標籤。
  3. 清除 [ 啟用匿名驗證] 核取方塊。
  4. 選取 [ 啟用 Windows 驗證] 核取方塊。
  5. 儲存並關閉屬性頁。

或者,您可以在檔案的 launchSettings.json 節點中 iisSettings 設定屬性:

"iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
        "applicationUrl": "http://localhost:52171/",
        "sslPort": 44308
    }
}

修改現有的專案時,請確認專案檔包含 Microsoft.AspNetCore.App 中繼套件Microsoft.AspNetCore.Authentication NuGet 套件的套件參考。

IIS

IIS 會使用ASP.NET Core 模組來裝載 ASP.NET Core應用程式。 Windows 驗證是透過 web.config 檔案來設定 IIS。 下列各節將示範如何:

  • 提供本機 web.config 檔案,以在部署應用程式時在伺服器上啟用 Windows 驗證。
  • 使用 IIS 管理員來設定已部署至伺服器之 ASP.NET Core應用程式的web.config檔案。

如果您尚未這麼做,請啟用 IIS 來裝載 ASP.NET Core應用程式。 如需詳細資訊,請參閱在使用 IIS 的 Windows 上裝載 ASP.NET Core

啟用 Windows 驗證的 IIS 角色服務。 如需詳細資訊,請參閱 在 IIS 角色服務中啟用 Windows 驗證 (請參閱步驟 2)

IIS 整合中介軟體 預設會設定為自動驗證要求。 如需詳細資訊,請參閱使用 IIS 在 Windows 上裝載 ASP.NET Core:IIS 選項 (AutomaticAuthentication)

ASP.NET Core模組預設會設定為將 Windows 驗證權杖轉送至應用程式。 如需詳細資訊,請參閱ASP.NET Core模組組態參考:aspNetCore 元素的屬性

使用下列 其中一 種方法:

  • 發佈和部署專案之前, 請將下列 web.config 檔案新增至專案根目錄:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <location path="." inheritInChildApplications="false">
        <system.webServer>
          <security>
            <authentication>
              <anonymousAuthentication enabled="false" />
              <windowsAuthentication enabled="true" />
            </authentication>
          </security>
        </system.webServer>
      </location>
    </configuration>
    

    當 .NET Core SDK 發行專案時, (專案檔中未 <IsTransformWebConfigDisabled> 將 屬性設定 true 為) ,則已發佈 的web.config 檔案會包含 區 <location><system.webServer><security><authentication> 段。 如需 屬性的詳細資訊 <IsTransformWebConfigDisabled> ,請參閱使用 IIS 在 Windows 上裝載 ASP.NET Core

  • 發佈和部署專案之後, 請使用 IIS 管理員執行伺服器端設定:

    1. 在 [IIS 管理員] 中,選取 [連線] 提要欄位 [月臺] 節點下的 [IIS台]。
    2. 按兩下IIS區域中的 [驗證]。
    3. 選取 [匿名驗證]。 在[動作] 提要欄中選取 [停用]。
    4. 選取 [Windows 驗證] 。 在[動作] 提要欄中選取 [啟用]。

    當採取這些動作時,IIS 管理員會修改應用程式的 web.config 檔案。 <system.webServer><security><authentication>已針對 和 windowsAuthentication 新增具有更新設定的 anonymousAuthentication 節點:

    <system.webServer>
      <security>
        <authentication>
          <anonymousAuthentication enabled="false" />
          <windowsAuthentication enabled="true" />
        </authentication>
      </security>
    </system.webServer>
    

    <system.webServer>IIS 管理員新增至web.config檔案的區段不在 .NET Core SDK 發佈的應用程式 <location> 區段之外。 因為區段會新增到節點外部 <location> ,所以設定會由任何 子應用程式 繼承至目前的應用程式。 若要避免繼承,請在 .NET Core SDK 提供的 區段內移動新增 <security><location><system.webServer> 區段。

    當 IIS 管理員用來新增 IIS 組態時,它只會影響伺服器上的應用程式 web.config 檔案。 如果伺服器的 web.config 複本由專案的 web.config 檔案取代,則後續部署的應用程式可能會覆寫伺服器上的設定。 使用下列 一方法來管理設定:

    • 使用 IIS 管理員在部署時覆寫檔案之後,重設 web.config 檔案中的設定。
    • 使用 設定在本機將web.config檔案 新增至應用程式。

Kestrel

Microsoft.AspNetCore.Authentication.Negotiate NuGet 套件可用於 Kestrel 在 Windows、Linux 和 macOS 上使用 Negotiate 和 Kerberos 來支援 Windows 驗證。

警告

認證可以在連線上跨要求保存。 除非 Proxy 維護 1:1 連線親和性,否則交涉驗證不得與 Proxy 搭配使用, (持續性 Kestrel 連線) 。

注意

Negotiate 處理常式會偵測基礎伺服器是否原生支援 Windows 驗證,以及是否已啟用。 如果伺服器支援 Windows 驗證,但已停用,則會擲回錯誤,要求您啟用伺服器實作。 在伺服器中啟用 Windows 驗證時,交涉處理常式會以透明方式將驗證要求轉送給它。

在 中 Startup.ConfigureServices 叫用 AddAuthenticationAddNegotiate 來新增驗證服務:

// using Microsoft.AspNetCore.Authentication.Negotiate;
// using Microsoft.Extensions.DependencyInjection;

services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
   .AddNegotiate();

在 中 Startup.Configure 呼叫 UseAuthentication 以新增驗證中介軟體:

app.UseAuthentication();

如需中介軟體的詳細資訊,請參閱ASP.NET Core中介軟體

Kerberos 驗證和角色型存取控制 (RBAC)

Linux 或 macOS 上的 Kerberos 驗證不會為已驗證的使用者提供任何角色資訊。 若要將角色和群組資訊新增至 Kerberos 使用者,驗證處理常式必須設定為從 LDAP 網域擷取角色。 最基本的設定只會指定要查詢的 LDAP 網域,並使用已驗證的使用者內容來查詢 LDAP 網域:

services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
    .AddNegotiate(options =>
    {
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        {
            options.EnableLdap("contoso.com");
        }
    });

某些設定可能需要特定認證才能查詢 LDAP 網域。 您可以在下列醒目提示的選項中指定認證:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));
    services.AddDatabaseDeveloperPageExceptionFilter();
    services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
        .AddEntityFrameworkStores<ApplicationDbContext>();

    services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
        .AddNegotiate(options =>
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                options.EnableLdap(settings =>
                {
                    settings.Domain = "contoso.com";
                    settings.MachineAccountName = "machineName";
                    settings.MachineAccountPassword = Configuration["Password"]
                });
            }
        });

    services.AddRazorPages();
}

根據預設,交涉驗證處理常式會解析巢狀網域。 在大型或複雜的 LDAP 環境中,解析巢狀網域可能會導致查閱速度緩慢,或每個使用者使用大量記憶體。 您可以使用 選項來 IgnoreNestedGroups 停用巢狀網域解析。

允許匿名要求。 使用ASP.NET Core授權來挑戰匿名要求進行驗證。

AuthenticationScheme 需要 NuGet 套件 Microsoft.AspNetCore.Authentication.Negotiate

Windows 環境設定

Microsoft.AspNetCore.Authentication.Negotiate元件會執行使用者模式驗證。 服務主體名稱 (SPN) 必須新增至執行服務的使用者帳戶,而不是電腦帳戶。 在系統管理命令殼層中執行 setspn -S HTTP/myservername.mydomain.com myuser

Linux 和 macOS 環境設定

如需將 Linux 或 macOS 電腦加入 Windows 網域的指示,請參閱使用 Windows 驗證 - Kerberos 將 Azure Data Studio 連線至您的SQL Server。 指示會在網域上建立 Linux 電腦的機器帳戶。 SPN 必須新增至該電腦帳戶。

注意

遵循使用Windows 驗證 - Kerberos 將 Azure Data Studio 連線至SQL Server中的指引時,請視需要將 取代 python-software-propertiespython3-software-properties 為 。

一旦 Linux 或 macOS 電腦加入網域,則需要額外的步驟,才能提供具有 SPN 的 keytab 檔案

  • 在網域控制站上,將新的 Web 服務 SPN 新增至電腦帳戶:
    • setspn -S HTTP/mywebservice.mydomain.com mymachine
    • setspn -S HTTP/mywebservice@MYDOMAIN.COM mymachine
  • 使用 ktpass 來產生 keytab 檔案:
    • ktpass -princ HTTP/mywebservice.mydomain.com@MYDOMAIN.COM -pass myKeyTabFilePassword -mapuser MYDOMAIN\mymachine$ -pType KRB5_NT_PRINCIPAL -out c:\temp\mymachine.HTTP.keytab -crypto AES256-SHA1
    • 某些欄位必須以大寫指定,如所述。
  • 將 keytab 檔案複製到 Linux 或 macOS 電腦。
  • 透過環境變數選取 keytab 檔案: export KRB5_KTNAME=/tmp/mymachine.HTTP.keytab
  • klist 用 以顯示目前可用的 SPN。

注意

keytab 檔案包含網域存取認證,因此必須據以保護。

HTTP.sys

HTTP.sys 支援使用 Negotiate、NTLM 或基本驗證 的核心模式 Windows 驗證。

在 中 Startup.ConfigureServicesAddAuthentication 用 (命名空間) Microsoft.AspNetCore.Server.HttpSys 來新增驗證服務:

services.AddAuthentication(HttpSysDefaults.AuthenticationScheme);

設定應用程式的 Web 主機,以搭配 Windows 驗證 Program.cs () 使用HTTP.sys。 UseHttpSys 位於 命名空間中 Microsoft.AspNetCore.Server.HttpSys

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>()
                    .UseHttpSys(options =>
                    {
                        options.Authentication.Schemes = 
                            AuthenticationSchemes.NTLM | 
                            AuthenticationSchemes.Negotiate;
                        options.Authentication.AllowAnonymous = false;
                    });
            });
}

注意

HTTP.sys使用 Kerberos 驗證通訊協定委派給 核心模式 驗證。 Kerberos 和HTTP.sys不支援使用者模式驗證。 必須使用電腦帳戶來解密 Kerberos 權杖/票證,該權杖/票證取自 Active Directory,並由用戶端將其轉送至伺服器來驗證使用者。 請註冊主機的服務主體名稱 (SPN),而非應用程式的使用者。

注意

Nano Server 1709 版或更新版本不支援HTTP.sys。 若要搭配 Nano Server 使用 Windows 驗證和HTTP.sys,請使用 Server Core (microsoft/windowsservercore) 容器。 如需 Server Core 的詳細資訊,請參閱 什麼是 Windows Server 中的 Server Core 安裝選項?

授權使用者

匿名存取的組態狀態會決定在應用程式中使用 和 [AllowAnonymous] 屬性的方式 [Authorize] 。 下列兩節說明如何處理匿名存取的不允許和允許的設定狀態。

不允許匿名存取

啟用 Windows 驗證並停用匿名存取時, [Authorize][AllowAnonymous] 屬性不會有任何作用。 如果 IIS 網站設定為不允許匿名存取,要求永遠不會到達應用程式。 基於這個理由, [AllowAnonymous] 屬性不適用。

允許匿名存取

啟用 Windows 驗證和匿名存取時,請使用 [Authorize][AllowAnonymous] 屬性。 屬性 [Authorize] 可讓您保護需要驗證之應用程式的端點。 屬性 [AllowAnonymous][Authorize] 覆寫應用程式中允許匿名存取的屬性。 如需屬性使用方式詳細資料,請參閱ASP.NET Core 中的簡單授權

注意

根據預設,缺少存取頁面授權的使用者會顯示空的 HTTP 403 回應。 StatusCodePages 中介軟體可以設定為為使用者提供更好的「拒絕存取」體驗。

模擬

ASP.NET Core不會實作模擬。 應用程式會使用應用程式集區或進程身分識別,針對所有要求使用應用程式的身分識別來執行。 如果應用程式應該代表使用者執行動作,請使用Windows Identity 。在中的終端機內嵌中介軟體Startup.Configure 中執行Impersonated 或 RunImpersonatedAsync 。 在此內容中執行單一動作,然後關閉內容。

app.Run(async (context) =>
{
    try
    {
        var user = (WindowsIdentity)context.User.Identity;

        await context.Response
            .WriteAsync($"User: {user.Name}\tState: {user.ImpersonationLevel}\n");

        WindowsIdentity.RunImpersonated(user.AccessToken, () =>
        {
            var impersonatedUser = WindowsIdentity.GetCurrent();
            var message =
                $"User: {impersonatedUser.Name}\t" +
                $"State: {impersonatedUser.ImpersonationLevel}";

            var bytes = Encoding.UTF8.GetBytes(message);
            context.Response.Body.Write(bytes, 0, bytes.Length);
        });
    }
    catch (Exception e)
    {
        await context.Response.WriteAsync(e.ToString());
    }
});

雖然 Microsoft.AspNetCore.Authentication.Negotiate 套件可在 Windows、Linux 和 macOS 上啟用驗證,但只有在 Windows 上才支援模擬。

宣告轉換

使用 IIS 裝載時, AuthenticateAsync 不會在內部呼叫 來初始化使用者。 因此,預設會在未啟動每個驗證之後,使用 IClaimsTransformation 實作來轉換宣告。 如需啟用宣告轉換的詳細資訊和程式碼範例,請參閱 進程內裝載和跨進程裝載之間的差異

其他資源