基于用户组限制WCF Web服务功能(Restrict WCF Web Service functi

2019-07-03 22:00发布

我有一个用C#客户端应用程序消费WCF Web服务。 我也有存储在Active Directory 4组。 客户端应用程序传递用户凭据连接该Web服务。

Web服务暴露多个API或方法来通过客户端应用程序如下访问:

    [OperationContract]
    bool Read();


    [OperationContract]
    bool Write();

阅读()方法应该是所有客户端访问

Write()方法只能由用户那些属于specifc由Active Directory维护Windows用户组可以访问。

问:我们如何可以过滤或限制客户端基于其用户群暴露接口或方法维持AD?


jrista,感谢您的回复。 我想同样的指令作为的PrincipalPermission如下:

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

[PrincipalPermission(SecurityAction.Demand, Role = "Writers")]
[OperationContract]
bool Write();

但是,这是行不通的。 阅读组用户也能够调用作家()方法和作家群体的用户也能够调用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>

它是实现此功能的wsHttpBinding要求? 如果是的话,那我怎么才能在我的Web服务实现的wsHttpBinding?

Answer 1:

我不知道把我的头顶部如何AD证书融入正常的.NET安全框架。 但是,它是可能的(我会看到,如果我能找到一些链接),一旦你这样做,你应该能够使用标准的安全属性来检查“角色”,这将符合您的AD组:

[OperationContract]
bool Read();

[PrincipalPermission(SecurityAction.Demand, Role = "Writers")]
[OperationContract]
bool Write();

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

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <adServiceBehavior>
        <serviceAuthorization principalPermissionMode="UseWindowsGroups" />
      </adServiceBehavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

有另外的想法。 有时候,愿望是,即使没有在接口上的Write()方法在所有。 有了WCF,可以实现在一个单一的服务类中的多个服务合同接口。 一个理想的解决方案可能是创建两个服务合同接口,其中一个阅读()和Write(),一个与刚才读()。 根据登录到客户端的用户,你可以使用那些谁只具有读访问的读取()接口,并为那些同时访问读取()/写()接口。 这也将让你露出最安全的服务合同,不应该有写权限的客户,同时利用读/写合同为内部管理目的。 你永远不暴露可能被潜在地利用这种方式的代码。



Answer 2:

jrista是正确的 - 你可以使用内置的Windows授权服务包括“的PrincipalPermission”属性来限制访问。

但是:之前你可以授权,则需要进行身份验证。 首先,你需要知道谁在敲你的服务的大门决定是否让或不是他(或她)面前。

为了做到这一点,你需要确保使用您的信息交换,以及客户端和服务器上的Windows凭据必须在同一个域(或结构域与相互信任的关系)。 此外,你需要使用像wsHttp或netTcp,允许和默认支持Windows凭据的绑定,你需要确保使用和配置跨越从客户端传输的Windows凭据到服务器绑定的安全配置。

你需要有类似:

<system.serviceModel>
  <bindings>
    <netTcpBinding>
      <binding name="Secured">
        <security mode="Transport">
          <transport clientCredentialType="Windows" />
        </security>
      </binding>
    </netTcpBinding>
  </bindings>
</system.serviceModel>

然后你需要从你的客户端和服务器端点引用绑定配置。

WsHttpBinding的和NetTcpBinding的都默认使用Windows客户端凭证,以便开箱即用,除非你已经变成安全完全关闭,你应该得到的Windows凭据在这两个绑定的支持。

PS:
作为jrista节目(和我做在前面的回答,以几乎同样的问题,你有),你真的只需要到的PrincipalPermission属性添加到要限制谁属于某一组用户的方法-没有手动与乱搞AD组成员等必要的。

如果你真的必须得到组,用户可以调用你的服务属于,你可以检查出的WindowsIdentity调用的“.Groups”属性:

WindowsIdentity winCaller = ServiceSecurityContext.Current.WindowsIdentity;
foreach(var group in winCaller.Groups)
{
   Console.WriteLine(group.Value);
}

如果您需要的用户的对骂中,使用winCaller.Name 。 如果您需要为用户呼叫的SID,使用winCaller.User 。 这一切都在那里 - 无需复杂,没有复杂的代码 - 只是用它! :-)



Answer 3:

如果你在IIS托管在服务器设置为允许匿名?



Answer 4:

尝试在服务界面的操作合同中服务类添加属性的PrincipalPermission的方法不行。



文章来源: Restrict WCF Web Service functionality based on User Group
标签: wcf service