2017-08-25 28 views
13

我有需要使用匿名和Windows和我一个AuthorizationProvider似乎无法获得那么Windows使用的质疑工作:强制Windows的质疑

if (principal == null || principal.Identity == null || string.IsNullOrWhiteSpace(principal.Identity.Name)) 
      { 
       context.OwinContext.Authentication.Challenge(); 
       return Task.FromResult(0); 
      } 

是否有我需要设置任何其他配置值为了这条线路工作? :context.OwinContext.Authentication.Challenge();

任何想法为什么这不起作用?我需要能够获得windows主体,只需启用windows即可正常工作,但还需要启用匿名才能够访问提供程序中的其他端点。

回答

4

简而言之,您应该在Web主机中启用Windows身份验证。 根据您使用的Web主机,有不同的设置。

配置完Web主机后,您的控制器代码开始工作。

OWIN自主机

配置HttpListener接受OWIN Startup两种认证方式类:

class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     var listener = (HttpListener)app.Properties["System.Net.HttpListener"]; 

     listener.AuthenticationSchemes = 
      AuthenticationSchemes.IntegratedWindowsAuthentication | 
      AuthenticationSchemes.Anonymous; 

     // Other initialization 
    } 
} 

IIS

如果你在IIS托管应用程序时,应在您的应用程序的IIS网站设置中启用Windows身份验证模式:

enter image description here enter image description here

如果你看不到验证图标或Windows身份验证模式,安装以下Windows功能:

enter image description here

的Visual Studio网络调试(IIS快递)

最后,为了方便从Visual Studio进行Web调试,您可以启用Windows aut在您的项目属性中进行验证。打开Solution Explorer中选择项目:

enter image description here

然后打开属性选项卡,并设置匿名和Windows身份验证:

enter image description here

欲了解更多详情,您可以检查出this article

+0

我在IIS中启用了Windows身份验证,但一旦我启用匿名以及我可以得到Windows用户帐户,只有启用匿名。这行应该提示windows声明context.OwinContext.Authentication.Challenge();但不起作用,所以不确定是否有其他设置需要设置。 – Fab

+0

@Fab客户端有什么?它是'HttpClient'吗? –

+0

@Fab btw尝试显式指定身份验证模式:'context.OwinContext.Authentication.Challenge(“Ntlm”)'。 –