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

时间:2016-06-08 05:50:04

标签: asp.net-core .net-core

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

我看到其他SO问题描述了承载身份验证,例如Bearer Authentication with ASP.NET Core RC2 404 instead of 403

但这不是我想要的。

3 个答案:

答案 0 :(得分:8)

您可以使用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以将WebListener与NTLM一起使用

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

答案 1 :(得分:4)

这在.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",

在中间件类中:

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

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

        ....
        ....

答案 2 :(得分:3)

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

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

要部署到iis,请查看此Asp.Net core MVC application Windows Authentication in IIS