繁体   English   中英

托管在 Windows 服务中的 Kestrel 的 Windows 身份验证

[英]Windows Authentication for Kestrel hosted in Windows service

我正在运行托管在 Windows 服务中的 ASP.NET Core 应用程序,如下所述:

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-2.1

我需要这个应用程序来支持 Windows 身份验证。 我有哪些选择? 我尝试使用 IIS 中的应用程序请求路由/ URL 重写模块作为具有 Windows 身份验证的反向代理,但无法弄清楚如何使其工作。 任何指导将不胜感激。

.NET Core Web 应用程序可以使用不同的 Web 服务器:

  • IIS Express(在 Visual Studio 中按 F5 时)
    支持 NTLM、协商 (Kerberos)
    仅限 Windows
  • IIS(部署到 IIS 文件夹时)
    支持NTLM,协商
    仅限 Windows
  • Kestrel(使用“dotnet run”或从命令行执行时)
    支持协商(带有 nuget 包,请参阅 Yush0s 回复)
    视窗/Linux
  • http.sys(与 kestrel 类似,但在 Startup.cs 中配置)
    支持NTLM,协商
    仅限 Windows

IIS / IIS Express 中的 Windows 身份验证可以正常工作。

Kestrel 只能使用协商 (Kerberos)。 这意味着您需要使用服务主体名称 (SPN) 设置受信任的连接。 这可以使用 setspn 命令行工具来完成。 不幸的是,我没有这方面的经验,因为在开发机器上你往往会跳过它。

http.sys 可以使用 NTLM,但与 IIS / IIS Express 不兼容。 这意味着您在使用它时不能使用 Visual Studio 调试。 作为一种解决方法,您可以添加一个决定是否使用 http.sys 的环境变量。 例如,将以下行添加到 launchSettings.json 中的“项目”配置文件:

  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "USE_HTTP_SYS": "true"
  }

现在可以有条件地使用 http.sys 或不:

 public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
        {
            if (bool.Parse(Environment.GetEnvironmentVariable("USE_HTTP_SYS") ?? "false"))
            {
                webBuilder.UseHttpSys(options =>
                {
                    options.Authentication.Schemes = AuthenticationSchemes.NTLM;
                    options.Authentication.AllowAnonymous = false;
                });
            }

            webBuilder.UseStartup<Startup>();
        });

作为旁注,由于 .NET Core 3.0 有一个新接口“IHostBuilder”而不是“IWebHostBuilder”(仅为向后兼容而存在)。


可悲的是,当您使用“dotnet run”并转到该网站时,您可能会看到一条错误消息:

  • Internet Explorer:无法安全连接到此页面
  • Chrome:无法访问此站点
  • 火狐:无法连接

Kestrel 带来了它自己的证书管理。 它在用户模式下运行并在“CurrentUser\My”中查找证书。 相比之下 http.sys 是内核模式,这意味着当前用户是未知的。 http.sys 在“LocalMachine\My”中查找证书。

因为 http.sys 不知道端口使用的是哪个证书,所以还需要将证书分配给 .net 应用程序的 https 端口。 这需要以本地管理员身份通过 PowerShell 完成:

$cert = Get-ChildItem -Path Cert:\LocalMachine\My | Where { $_.Subject -eq "CN=localhost" } | Select -First 1
netsh http add sslcert ipport=0.0.0.0:5001 appid='{12345678-1234-43f8-a778-f05f3c6b9709}' certhash=$($cert.Thumbprint)

请注意,“CN=localhost”是 uri,“0.0.0.0:5001”是 dotnet 应用程序的端口,appid 是随机标识符(如果您的应用程序有 guid,您也可以使用它,但这不是必需的) .

如果您没有证书(例如用于开发),您可以为机器创建一个自签名证书(需要 Win10 和管理员权限):

$rootextension = [System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension]::new($true, $true, 0, $true)
$cert = New-SelfSignedCertificate -Subject "CN=localhost" -FriendlyName "Development localhost Certificate" -Extension $rootextension -NotAfter ([DateTime]::Now).AddYears(10) -KeyUsage DigitalSignature,KeyEncipherment,DataEncipherment -CertStoreLocation "Cert:\LocalMachine\My" -KeyExportPolicy Exportable

使用 .Net Core 3.0,您可以将 Windows 身份验证与 Kestrel 一起使用。 它有一个 Nuget 包: Microsoft.AspNetCore.Authentication.Negotiate

然后你可以在 Startup.ConfigureServices 中添加它:

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

有关更多信息,另请参阅:
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/windowsauth?view=aspnetcore-3.0&tabs=visual-studio#kestrel

Microsoft 有一整篇关于ASP.NET Core 中的 Windows 身份验证的文章,其中包括描述如何在没有 IIS 的情况下进行身份验证的部分 Kestrel 不支持 Windows 身份验证 更新:现在支持),因此您必须使用 HTTP.sys 进行托管。 起初看起来很容易(在您的 Program.cs 中):

.UseHttpSys(options =>
{
    options.Authentication.Schemes = 
        AuthenticationSchemes.NTLM | AuthenticationSchemes.Negotiate;
    options.Authentication.AllowAnonymous = false;
})

直到您意识到还有另一篇关于在 HTTP.sys 中托管的文章,所以您可能会发现一些其他原因,它可能会破坏其他内容。

在 IIS(而不是 Windows 服务)中托管它让 IIS 处理 Windows Authentication可能更容易。

您决定首先托管在 Windows 服务中是有原因的吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM