asp.net 5中的Windows身份validation

我正在ASP .NET 5,MVC 6中构建一个Intranet应用程序。我想知道如何启用Windows身份validation。 默认项目模板仅支持单个用户帐户。

Mark的答案在ASP.Net RC1中仍然有效。 还有一些额外的步骤可以将它们组合在一起(我没有足够的声誉来评论他的解决方案):

  1. 从NuGet安装WebListener
  2. 将以下用法添加到Startcup.cs:

    using Microsoft.AspNet.Http.Features; using Microsoft.Net.Http.Server; 
  3. 在app.UseMvc之前的Configure方法中添加Mark的代码片段 :

     // 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(); if (listener != null) { listener.AuthenticationManager.AuthenticationSchemes = AuthenticationSchemes.NTLM; } 
  4. 要调试它,您需要在project.json 添加WebListener运行目标 ,正如Mark在另一个答案中所述:

     "commands": { "weblistener": "Microsoft.AspNet.Server.WebListener --config hosting.ini", "web": "Microsoft.AspNet.Server.Kestrel" }, 
  5. 选择weblistener而不是Web(Kestrel)的IIS Express来调试您的应用程序。

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

更新BETA 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(); if (listener != null) { listener.AuthenticationManager.AuthenticationSchemes = AuthenticationSchemes.NTLM; } 

以前对BETA的回答7

 // If we're self-hosting, enable windows/integrated authentication. // For IIS, this needs to be configured in IIS instead, and the // following will have no effect. if ((app.Server as ServerInformation) != null) { var serverInformation = (ServerInformation)app.Server; serverInformation.Listener.AuthenticationManager.AuthenticationSchemes = AuthenticationSchemes.NTLM; } 

改编自官方MusicStore示例 。

如果您正在使用带有IIS Express的Visual Studio 2015进行调试,则可以通过项目的调试属性页中的复选框立即打开Windows身份validation,而不是弄乱applicationhost.config文件。 我无法让web.config解决方案适用于IIS Express调试,它会抛出有关配置在该级别无效的错误。 请注意,目前这不适用于测试版8 – 请参阅此问题

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

以下是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 ,则必须自己将authentication节点添加到web.config 。 如果您正在使用HttpPlatformHandler (我个人推荐)并代理到forwardWindowsAuthToken="true" ,请将forwardWindowsAuthToken="true"添加到web.confighttpPlatform节点。

使用IIS主机,您可以使用适用于您的应用程序的IIS配置将web.config文件添加到wwwroot目录中。

web.config中

            

我做了我在互联网上找到的一切,没有人工作。 所以,我查看了aspnet 4.5配置文件,我看到它使用:

 disabled enabled 

在.csproj文件中,我刚刚复制到aspnet 5的.xproj文件,它工作正常。

由于您正在构建新应用程序,因此可以通过单击“ Change AuthenticationChange Authentication 这将显示一个选项,您可以在其中将类型类型更改为Windows身份validation。

在此处输入图像描述 在此处输入图像描述

对于来自空Web应用程序的RC1和IISExpress:

  • 右键单击Web项目,选择“ Properties
  • 单击Debug选项卡,选中Enable Windows Authentication

这会影响~/Properties/launchSettings.json ,如下所示:

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

如果要在当前Web项目上启用Windows身份validation:

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

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

并设置“Windows身份validation”

运行项目,一切都会好的。

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

在配置标签中添加: