在Asp.net WebApi中使用AD组进行Windows身份验证无法正常工作

时间:2012-09-21 14:03:51

标签: asp.net-mvc-3 asp.net-web-api windows-authentication

我使用Asp.net WebAPI开发了Restful webservice,并在web.config文件中使用了windows身份验证。此WebService由内部网络中的内部窗口应用程序调用。现在问题是当我发送请求时 IE浏览器到webService它认证和授权工作正常。 但是当我通过Widows应用程序发送请求时,用户未经过身份验证。 场景:    当我使用下面的配置时,来自webbrowser的请求被正确验证,但即使用户在具有正确角色的正确AD组中,来自Windows应用程序的请求也未经过身份验证。

   <authentication mode="Windows"/>
    <authorization>
       <allow roles = "someGroup1"/>
       <allow roles = "Somegroup2"/>
       <deny users="*" />
    </authorization>

可能是什么问题请提供任何建议。

1 个答案:

答案 0 :(得分:3)

我发现了问题,Windows客户端没有将用户信息发送到Webhost,因此身份验证和授权失败。由于我使用的是NTML身份验证,因此要解决此问题,我必须向restClient添加一个Authenticator,如下所示:

var client = new RestClient();
     client.BaseUrl = ServiceUrl;
     client.RemoveHandler("application/json");
     client.RemoveHandler("text/json");
     **client.Authenticator = new NtlmAuthenticator();** 
相关问题