微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

[OperationContract]
    bool Read();


    [OperationContract]
    bool Write();

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

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

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

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

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

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

但它不起作用.读组用户也可以调用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>

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

解决方法

我不确定如何将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,您可以在单个服务类上实现多个服务契约接口.一个理想的解决方案可能是创建两个服务契约接口,一个使用Read()和Write(),一个只使用Read().根据登录到客户端的用户,您可以对只有读访问权限的用户使用Read()接口,对有权访问这些用户的人使用Read()/ Write()接口.这还允许您向不应具有写访问权限的客户端公开最安全的服务合同,同时在内部使用读/写合同进行管理.您永远不会暴露可能以这种方式被利用的代码.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐