繁体   English   中英

没有SSL但具有Windows组身份验证的WCF服务

[英]WCF service without SSL but with Windows Group authentication

我们正在尝试创建只能由指定的Windows组访问的WCF服务。 如何在服务器web.config和客户端配置中配置?

注意:我们希望能够控制允许在服务器web.config中访问的窗口组,而不是代码。 此外,我们根本不需要/需要SSL。

我用Google搜索,然后我能找到的最好的例子都是这样的......

WCF服务,Windows身份验证

但这并不能解释如何限制只访问特定组或组。

如果这是Intranet应用程序,您可以使用netTcpBinding:

<services>
   <service name="YourService"
      behaviorConfiguration="YourServiceBehavior">
      <endpoint 
         binding="netTcpBinding"
         bindingConfiguration="SecureTransportWindows"
         contract="YourContract" />
   </service>
</services>

<bindings>
   <binding name="SecureTransportWindows">
      <security mode="Transport">
          <transport clientCredentialType="Windows" />
      </security>
   </binding>
</bindings>

<behaviors>
   <serviceBehaviors>
      <behavior name="YourServiceBehavior">          
          <serviceAuthorization principalPermissionMode="UseWindowsGroups" />
      </behavior>
   </serviceBehaviors>
</behaviours>

然后在服务代码中,您可以要求Windows角色:

class YourService : YourContract
{
    [PrincipalPermission(SecurityAction.Demand, Role="MYDOMAIN\Administrators")]
    public string SecuredOperation(string name)
    {
       return "secured operation";
    }
}

如果需要在config中进行设置,则必须实现自定义授权:

<behavior name="YourServiceBehavior">          
   <serviceAuthorization principalPermissionMode="Custom">            
      <authorizationPolicies>
         <add policyType="YourCustomAuthorizationPolicy"/>
      </authorizationPolicies>          
   </serviceAuthorization>
</behavior>

并在代码实现IAuthorizationPolicy接口:

public class YourCustomAuthorizationPolicy : IAuthorizationPolicy
{
   //you need to check msdn 
}

好的,这是我们提出的解决方案。 虽然它确实涉及代码更改(添加AspNetCompatibilityRequirements属性),但我们现在可以在web.config文件中实现组/角色的配置,而不是硬编码。

这有很多步骤......

1)将aspNetCompatibilityEnabled属性添加到serviceHostingEnvironment元素并设置为true,例如......

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

这告诉WCF服务在ASP.NET兼容模式下运行并完全参与ASP.NET HTTP请求生命周期。 有关完整详细信息,请参阅此MSDN文章

2)在WCF代码中,根据上面的链接和本MSDN文章中的规定,将AspNetCompatibilityRequirements属性添加到服务类中......

<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>

3)现在我们可以添加通常的ASP 授权元素来限制对指定组/用户的访问(没有上面的设置(1)和(2),这将被WCF忽略)...

<system.web>
    <authorization>
        <allow roles="MYDOMAIN\WCFAuthenticatedUsers" /> <-- allows access to users in this group
        <deny users="*" /> <-- denies access to all other users
    </authorization>
</system.web>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM