我们正试图创建一个WCF服务,该服务只能被指定的Windows组访问。 如何在服务器web.config和客户端配置中配置?WCF服务不带SSL,但带有Windows组认证
注意:我们希望能够控制允许在服务器web.config中访问但不在代码中的Windows组。另外,我们根本不需要SSL。
伊夫用Google搜索周围,然后我能找到都是这样最好的例子...
WCF Service, Windows Authentication
但是,这并不说明如何限制访问仅向特定的群体或团体。
我们正试图创建一个WCF服务,该服务只能被指定的Windows组访问。 如何在服务器web.config和客户端配置中配置?WCF服务不带SSL,但带有Windows组认证
注意:我们希望能够控制允许在服务器web.config中访问但不在代码中的Windows组。另外,我们根本不需要SSL。
伊夫用Google搜索周围,然后我能找到都是这样最好的例子...
WCF Service, Windows Authentication
但是,这并不说明如何限制访问仅向特定的群体或团体。
好的,这是我们想出的解决方案。虽然它确实涉及代码更改(添加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 授权元素来限制访问指定的组/用户(无设置(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>
如果这是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>
然后在服务代码,你可以要求窗口作用:
class YourService : YourContract
{
[PrincipalPermission(SecurityAction.Demand, Role="MYDOMAIN\Administrators")]
public string SecuredOperation(string name)
{
return "secured operation";
}
}
如果您需要设置它的配置,那么你必须实现自定义授权:
<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属性中。我们现在想出了一个解决方案,我会写出作为替代方案的解决方案,以便让其他人受益。谢谢 – barrylloyd
你为什么不希望SS L·我知道有一些合理的原因,但你应该说明他们 – Seph
好吧,这是一个很好的问题。基本上这是一个Intranet应用程序,公司不希望在客户机上维护SSL证书的开销/成本。如果这个问题只能通过SSL解决,那么我们可能不得不重新考虑这一点 - 但我希望我们可以避免它。 – barrylloyd
您使用哪种绑定? –