基于用户组限制 WCF Web 服务功能

Lal*_*rma 5 service wcf

我有一个由 C# 客户端应用程序使用的 WCF Web 服务。我还在 Active Directory 中存储了 4 个组。客户端应用程序正在传递用户凭据以连接此 Web 服务。

Web 服务公开多个 API 或方法供客户端应用程序访问,如下所示:

    [OperationContract]
    bool Read();


    [OperationContract]
    bool Write();
Run Code Online (Sandbox Code Playgroud)

Read() 方法应该可供所有客户端访问

只有属于 Active Directory 维护的特定 Windows 用户组的用户才能访问 Write() 方法。

问题:我们如何根据客户端在 AD 中维护的用户组来过滤或限制客户端暴露的接口或方法?


jrista,感谢您的回复。我尝试了与 PrincipalPermission 相同的指令,如下所示:

[PrincipalPermission(SecurityAction.Demand, Role = "Readers")]
[OperationContract]
bool Read();

[PrincipalPermission(SecurityAction.Demand, Role = "Writers")]
[OperationContract]
bool Write();
Run Code Online (Sandbox Code Playgroud)

但它不起作用。Read 组用户也可以调用 Writer() 方法,Writer 组用户也可以调用 Write() 方法。

我想告诉你的一件事是,我在 web.config 文件中使用 BasicHttpBind,如下所示:

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBind">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" proxyCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="DXDirectory.DXDirectoryService" behaviorConfiguration="DXDirectory.Service1Behavior">
        <!-- Service Endpoints -->
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBind"
                  name="BasicBinding" contract="DXDirectory.IDXDirectoryService">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DXDirectory.Service1Behavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
          <serviceAuthorization principalPermissionMode="UseWindowsGroups"/>          
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

是否需要为此功能实现 wsHttpBinding?如果是,那么如何在我的 Web 服务中实现 wsHttpBinding?

jri*_*sta 5

我不确定如何将 AD 凭据集成到普通的 .NET 安全框架中。但是,这是可能的(我会看看我是否能找到一些链接),一旦你这样做了,你应该能够使用标准安全属性来检查与你的 AD 组相对应的“角色”:

[OperationContract]
bool Read();

[PrincipalPermission(SecurityAction.Demand, Role = "Writers")]
[OperationContract]
bool Write();
Run Code Online (Sandbox Code Playgroud)

要利用 AD 组,请配置服务行为:

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <adServiceBehavior>
        <serviceAuthorization principalPermissionMode="UseWindowsGroups" />
      </adServiceBehavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

又有了想法。有时甚至希望接口上根本没有 Write() 方法。使用 WCF,您可以在单个服务类上实现多个服务协定接口。一个理想的解决方案可能是创建两个服务契约接口,一个使用 Read() 和 Write(),一个只使用 Read()。根据登录到客户端的用户,您可以将 Read() 接口用于只有读取访问权限的用户,而 Read()/Write() 接口用于具有两者访问权限的用户。这还允许您将最安全的服务合同公开给不应具有写访问权限的客户端,同时在内部使用读/写合同进行管理。您永远不会公开可能以这种方式被利用的代码。