Windows 使用 asp.net 核心进行身份验证

Windows Authentication with asp.net core

请提供有关如何在 ASP.NET 核心 RC2+ 上实施 Windows 身份验证的指导。

我看到其他描述承载身份验证的 SO 问题,例如

但这不是我要找的。

您可以使用 WebListener 执行此操作,如下所示:

  1. 打开您的 project.json 并将 WebListener 添加到依赖项:

    "dependencies" : {
      ...
      "Microsoft.AspNetCore.Server.WebListener": "0.1.0-rc2-final"
      ...
    }
    
  2. 将 WebListener 添加到命令(再次在 Project.json 中)

      "commands": {
        "weblistener": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener"
      },
    
  3. 在Startup.cs中,指定WebHostBuilder使用带有NTLM的WebListener

     var host = new WebHostBuilder()
            // Some configuration
            .UseWebListener(options => options.Listener.AuthenticationManager.AuthenticationSchemes = AuthenticationSchemes.NTLM)
            // Also UseUrls() is mandatory if no configuration is used
            .Build();
    

就是这样!

这在 .Net Core 1.0.0 (RTM) 中似乎不再有效。我在 Ivan Prodanov 的回答中完全按照上面的方式执行 WebHostBuilder ;它运行,不会在那里出现错误,但是 HttpContext.User 没有用 WindowsIdentity 标记。以下代码用于 ASP.Net 5 beta6:

在project.json中:

"version": "1.0.0"
"dependencies": {
  "Microsoft.AspNetCore.Owin": "1.0.0",
  "Microsoft.AspNetCore.Server.WebListener": "0.1.0",

在中间件中 class:

public async Task Invoke(HttpContext context)
{
    try
    {
        ClaimsPrincipal principal = context.User;

// <-- get invalidcastexception here:
        WindowsIdentity winIdentity = (WindowsIdentity)principal.Identity;  

        ....
        ....

检查您的 launchSettings.json 文件 - 将 anonymousAuthentication 更改为 false

  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,

要部署到 iis,请检查此