asp.net 5中的Windows身份验证

Ana*_*i M 16 c# windows-authentication asp.net-core-mvc asp.net-core

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

zem*_*ien 7

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

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

    using Microsoft.AspNet.Http.Features;
    using Microsoft.Net.Http.Server;
    
    Run Code Online (Sandbox Code Playgroud)
  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<WebListener>();
    if (listener != null)
    {
        listener.AuthenticationManager.AuthenticationSchemes = 
        AuthenticationSchemes.NTLM;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 要调试这一点,你需要添加WebListener运行目标project.json,马克在一个不同的答案注意:

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


Mar*_*hes 5

除了这里仅用于IIS托管的其他答案之外,您还可以通过在Startup.cs Configure方法中添加以下内容,在自托管的ASP.NET 5项目中启用Windows身份验证(针对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<WebListener>();
if (listener != null)
{
    listener.AuthenticationManager.AuthenticationSchemes = 
        AuthenticationSchemes.NTLM;
}
Run Code Online (Sandbox Code Playgroud)

以前对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;
}
Run Code Online (Sandbox Code Playgroud)

改编自官方MusicStore示例.

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


Joa*_*anc 5

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

以下是一个例子 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"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但也使用扩展app.UseIISPlatformHandler();而不是操纵监听器.该扩展将设置一个中间件,它将自动请求NTLM并从IIS转换适当的句柄.

部署到IIS时,如果您正在使用,则WebListener必须自己添加authentication节点web.config.如果您正在使用HttpPlatformHandler(我个人推荐)并代理到kestrel,请添加forwardWindowsAuthToken="true"到该httpPlatform节点中web.config.


小智 0

您需要手动配置IIS Express(在VS2015 CTP6中)。为此,请编辑 applicationhost.config 文件。(C:\Users\您的用户名\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>
Run Code Online (Sandbox Code Playgroud)