1

尝试在测试环境中实现主题。ASP.NET Core中特定路由上的NTLM身份验证

.UseWebListener(options=> 
      { 
       options.ListenerSettings.Authentication.Schemes = AuthenticationSchemes.NTLM | 
                    AuthenticationSchemes.Negotiate; 
       options.ListenerSettings.Authentication.AllowAnonymous = true; 
      }) 

而且

app.UseWhen(context => context.Request.Path.StartsWithSegments("/ntlm"), 
       builder => builder.UseCookieAuthentication(new CookieAuthenticationOptions() 
        { 
         AutomaticAuthenticate = true, 
         AutomaticChallenge = true, 
         LoginPath = "/Main/Login", 
        LogoutPath = "/Main/Logout", 
        AuthenticationScheme = "NTLM", AccessDeniedPath = "/Main/Deny" 
        } 
       )); 
      app.UseWhen(context => !context.Request.Path.StartsWithSegments("/ntlm"), 
       builder => builder.UseCookieAuthentication(new CookieAuthenticationOptions() 
       { 
        AutomaticAuthenticate = false, 
        AutomaticChallenge = false, 
        LoginPath = "/Main/Login", 
        LogoutPath = "/Main/Logout", 
        AuthenticationScheme = "Cookies" 
       } 
      )); 

但似乎可以去看没有区别,请求路径是否符合 “/ NTLM” 或不启动。

我试着运行两个WebListeners,但我认为有更多的开销。

我想要达到的目标: 用户登录开始页面,登录表单上有一个“Windows验证”按钮。 他可以输入凭据或按下按钮并以他的操作系统身份进入。

回答

1

我正在做一些非常类似于使用IIS的东西,而不是WebListener,但也许我可以告诉你一些可以提供帮助的东西。

您已配置WebListener,就像我为我的IIS所做的那样,以允许匿名访问,但也能够协商认证,该部分应该没问题。

但是在“/ ntlm”url路径中,您已经安装了一个CookieAuthentication中间件,该中间件将尝试在传入请求中查找用于认证用户的cookie,而我认为这不是您想要的。相反,在“/ ntlm”路径上,您希望重新使用来自WebListener检测到的NTLM或Kerberos数据包的身份。就我而言,正确设置时,它是负责设置身份的IIS中间件。我建议:

  • 除去时的“NTLM”路径
  • 制造“[授权]”的控制器,并用一个动作归因于触发认证
  • 显示HttpContext.User.Identity.Name;
  • 希望这UseCookieAuthentication你会得到Windows用户在这里正确认证
+0

魔法!这是正确的方法。 谢谢! – Danil

+0

很酷。对于您想通过cookie保护的操作,您可以指定[授权(ActiveAuthenticationSchemes =“Cookie”)]以确保这是用于设置用户身份的中间件。 – Daboul