C# asp.net 5中的Windows身份验证

C# asp.net 5中的Windows身份验证,c#,windows-authentication,asp.net-core,asp.net-core-mvc,C#,Windows Authentication,Asp.net Core,Asp.net Core Mvc,我正在用ASP.NET5、MVC6构建一个内部网应用程序。我想知道如何启用Windows身份验证。?默认项目模板仅支持单个用户帐户 您需要手动配置IIS Express(在VS2015 CTP6中)。 为此,请编辑applicationhost.config文件。 (C:\Users\your username\Documents\IISExpress\config\applicationhost.config) 在配置标签中添加以下内容: <location path="{your si

我正在用ASP.NET5、MVC6构建一个内部网应用程序。我想知道如何启用Windows身份验证。?默认项目模板仅支持单个用户帐户

您需要手动配置IIS Express(在VS2015 CTP6中)。 为此,请编辑applicationhost.config文件。 (C:\Users\your username\Documents\IISExpress\config\applicationhost.config)

在配置标签中添加以下内容:

<location path="{your site name}">
    <system.webServer>
        <security>
            <authentication>
                <windowsAuthentication enabled="true" />
                <anonymousAuthentication enabled="false" />
            </authentication>
        </security>
    </system.webServer>
</location>

使用IIS托管,您可以使用应用程序的IIS配置将web.config文件添加到wwwroot目录

web.config

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

我做了我在互联网上找到的所有事情,没有人工作。因此,我查看了aspnet 4.5配置文件,发现它使用:

<IISExpressAnonymousAuthentication>disabled</IISExpressAnonymousAuthentication>
<IISExpressWindowsAuthentication>enabled</IISExpressWindowsAuthentication>
已禁用
启用

在.csproj文件上,我刚刚复制到了aspnet 5的.xproj文件,它成功了。

除了这里的其他答案(仅适用于IIS托管)之外,您还可以通过在Startup.cs
Configure
方法中添加以下内容,在自托管ASP.NET 5项目(针对beta 7和beta 8测试)中启用Windows身份验证:,在
应用程序之前,请使用MVC
或您希望保护的类似程序:

测试版8的更新

// If we're self-hosting, enable integrated authentication (if we're using
// IIS, this will be done at the IIS configuration level).
var listener = app.ServerFeatures.Get<WebListener>();
if (listener != null)
{
    listener.AuthenticationManager.AuthenticationSchemes = 
        AuthenticationSchemes.NTLM;
}
改编自官方

如果您正在使用Visual Studio 2015和IIS Express进行调试,您现在可以通过项目的“调试属性”页面中的复选框启用Windows身份验证,而不必处理
applicationhost.config
文件。我无法使web.config解决方案用于IIS Express调试,它会抛出一个关于配置在该级别无效的错误注意,这在beta 8中目前不起作用-请参见 Mark的答案在ASP.NETRC1中仍然有效。还有一些额外的步骤可以将这一切联系起来(我没有足够的声誉来评论他的解决方案):

  • 安装
  • 将以下用法添加到Startcup.cs:

    使用Microsoft.AspNet.Http.Features;
    使用Microsoft.Net.Http.Server;
    
  • 在app.UseMvc之前添加Configure方法:

    //如果我们是自托管,请启用集成身份验证(如果我们使用
    //IIS,这将在IIS配置级别完成)。
    var listener=app.ServerFeatures.Get();
    if(侦听器!=null)
    {
    listener.AuthenticationManager.AuthenticationSchemes=
    AuthenticationSchemes.NTLM;
    }
    
  • 要调试此功能,您需要在
    project.json
    中执行以下操作,如Mark在另一个回答中所述:

    “命令”:{
    “weblistener”:“Microsoft.AspNet.Server.weblistener--config hosting.ini”,
    “web”:“Microsoft.AspNet.Server.Kestrel”
    },
    
  • 选择weblistener而不是IIS Express of web(Kestrel)来调试应用程序


  • 因为您正在构建一个新的应用程序,所以可以通过单击
    change authentication
    来更改身份验证类型。这将显示一个选项,您可以在其中将类型更改为Windows身份验证


    对于RC1和IISExpress,请从空的Web应用程序:

    • 右键单击web项目,选择
      Properties
    • 单击调试选项卡,选中启用Windows身份验证
    这影响了
    ~/Properties/launchSettings.json
    ,如下所示:

    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    

    如果要在当前web项目上启用windows身份验证:

    在解决方案资源管理器上,右击网站并选择“属性窗口”

    将“匿名身份验证”设置为“已禁用”

    并设置“Windows身份验证”

    运行项目,一切都会正常。

    根据启动设置设置
    节点的IISExpress进行适当调试时,
    $(ProjectDir)\Properties\launchSettings.json
    文件将触发Visual Studio生成
    web.config
    文件

    下面是一个示例
    launchSettings.json

    {
      "iisSettings": {
        "windowsAuthentication": true,
        "anonymousAuthentication": false,
        "iisExpress": {
          "applicationUrl": "http://localhost:65070/",
          "sslPort": 0
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "Hosting:Environment": "Development"
          }
        },
        "web": {
          "commandName": "web",
          "environmentVariables": {
            "Hosting:Environment": "Development"
          }
        }
      }
    }
    
    但也可以使用扩展名app.UseIISPlatformHandler()而不是操纵侦听器。扩展将设置一个中间件,该中间件将自动请求NTLM并从IIS转换适当的句柄


    部署到IIS时,如果使用的是
    WebListener
    ,则必须将
    身份验证
    节点添加到
    web.config
    。如果您正在使用
    HttpPlatformHandler
    (我个人推荐)并代理kestrel,请将
    forwardWindowsAuthToken=“true”
    添加到
    web.config
    中的
    httpPlatform
    节点

    如果您计划在IIS上托管您的站点,您可以在IIS中配置windows身份验证。关于其他选项,如自托管..?请编写一个中间件Iguess@user2935752我知道这已经有一段时间了,但如果您仍在寻找,请参阅我关于如何为自托管项目启用此功能的答案。
    {
      "iisSettings": {
        "windowsAuthentication": true,
        "anonymousAuthentication": false,
        "iisExpress": {
          "applicationUrl": "http://localhost:65070/",
          "sslPort": 0
        }
      },
      "profiles": {
        "IIS Express": {
          "commandName": "IISExpress",
          "launchBrowser": true,
          "environmentVariables": {
            "Hosting:Environment": "Development"
          }
        },
        "web": {
          "commandName": "web",
          "environmentVariables": {
            "Hosting:Environment": "Development"
          }
        }
      }
    }