【问题标题】:WCF service without SSL but with Windows Group authentication没有 SSL 但具有 Windows 组身份验证的 WCF 服务
【发布时间】:2012-05-15 03:45:41
【问题描述】:

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

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

我用谷歌搜索了一下,然后我能找到的最好的例子都是这样的......

WCF Service, Windows Authentication

但这并不能解释如何将访问权限仅限于特定组。

【问题讨论】:

  • 你为什么不想要 SSL?我知道有正当理由,但您应该说明原因
  • 好的,这是个好问题。基本上这是一个 Intranet 应用程序,公司不希望在客户端机器上维护 SSL 证书的开销/成本。如果这个问题只能通过 SSL 解决,那么我们可能不得不重新考虑——但我希望我们可以避免它。
  • 我们目前正在使用 wsHttpBinding

标签: wcf ssl web-config windows-authentication


【解决方案1】:

如果这是 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 
}

【讨论】:

  • 感谢 jlp,这很有帮助,但我们真的想在配置文件中控制有权访问的组/角色,而不是硬编码到代码中的 PrincipalPermission 属性中。我们现在提出了一个解决方案,我将把它写成替代答案,以防它对其他人有益。谢谢
【解决方案2】:

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

这有很多步骤...

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

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

这告诉 WCF 服务在 ASP.NET 兼容模式下运行并完全参与 ASP.NET HTTP 请求生命周期。详情请参阅this MSDN article

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

<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>

3) 现在我们可以添加通常的 ASP authorization 元素来限制对指定组/用户的访问(没有上面的设置(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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    相关资源
    最近更新 更多