如何使用Windows身份验证和注册自定义身份验证管理器

时间:2011-08-01 17:55:39

标签: .net iis-7.5 asp.net-4.0 windows-authentication impersonation

我正致力于保护RESTFul服务。

所有客户端都是Windows Intranet客户端,我想使用Windows集成身份验证。作为初始实现,我想对最终用户的凭据执行授权检查,并允许用户访问服务上的“读取”方法,并允许访问服务帐户上的写入方法。我正在经历许多不同的问题,以使任何这项工作。我已经在不同的地方阅读了大量的关节,但到目前为止还没有人知道。 以下是应用程序的web.config中的配置。

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Windows" />
    <identity impersonate="true" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <protocolMapping>
      <add scheme="http" binding="webHttpBinding" />
    </protocolMapping>
    <services>
      <service name ="SecureREST" behaviorConfiguration="RESTAuthorisationBehaviour">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="default" contract="Service.ISecureService">
        </endpoint>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="default">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Ntlm" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RESTAuthorisationBehaviour">
          <serviceAuthorization serviceAuthorizationManagerType="Security.RESTAuthorisationManager"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

在IIS中,我禁用了匿名身份验证。我需要这样做,因为我们希望所有客户端都经过身份验证,以便我们可以执行授权检查。我还启用了Windows身份验证(NTLM?)。

我遇到的第一个问题是当我从浏览器测试时,我收到以下错误消息:

  

此服务的安全设置需要“匿名”身份验证,但不会为承载此服务的IIS应用程序启用此功能。

我不想启用匿名身份验证,因此看起来在其他地方有一些配置指示Anon身份验证,这显然与我已禁用它的事实不一致。我还需要在哪里指定不允许匿名身份验证? (您还会看到我没有使用任何根据各种文章可能导致此问题的mex端点。)

我的下一个问题是我正在尝试注册自定义授权管理器。这样做的原因是我想检查用户所属的组,并根据该组做出授权决定。例如,如果用户是名为“ReadOnly”的组的成员,那么他们只能在服务上调用get方法。如果客户端是组'ReadAndWrite'的成员,那么他们也可以在服务上调用put方法。

你应该看到我试图注册一个授权类(派生自ServiceauthorisationManager);这是在RESTAuthorisationBehaviour行为中表达的。我已经在这个类中添加了一些日志记录,但日志中没有显示这些日志,因此我知道它没有正确注册。

最后一个问题是假冒。为了测试这一点,在服务代码中,我正在访问WindowIdentity并将其显示在日志中。我发现的是用户名不代表我需要的最终用户,所以我可以执行前面描述的授权检查。相反,我所看到的是用户是应用程序池标识(' IIS APPPOOL \ SecureREST ')。这对我没用。我已经打开了impersonate标志(正如您从配置中看到的那样),但是如何让它工作以便在WindowsIdentity对象中表示真实的客户端凭据?我用来获取windows身份的代码如下:

WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
string accountName = windowsIdentity.Name;

我也试过在windowsIndentity对象上调用Impersonate方法无济于事。

那么我该如何冒充经过身份验证的用户?

1 个答案:

答案 0 :(得分:0)

关于匿名身份验证的例外,请尝试在服务行为中禁用http get元数据,如下所示:

    <behavior name="MyServiceBehavior">
      <serviceMetadata httpGetEnabled="false" />
    </behavior>