WCF数据契约授权
如何在类上使用 [PrincipalPermission(SecurityAction.Demand, Role = "Administrators")]
属性?
我正在寻找某种方法来限制对我的对象的访问 IE 如果在服务方法中访问某个对象,并且用户有权访问该服务方法但无权访问该对象,则应抛出异常
how to use [PrincipalPermission(SecurityAction.Demand, Role = "Administrators")]
attribute on a class?
I am looking for some way to restrict the access on my object
i.e
if some object is being accessed in a service method and if the user has rights for accessing the service method but does not have rights accessing the object an exception should be thrown
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PrincipalPermission
属性可以装饰方法或类。因此,可以限制对对象实例的访问。需要完成几件事:Windows
作为客户端凭据类型。PrincipalPermission
属性的机密信息的装饰类。如果需要将单例实例传递给
ServiceHost
构造函数,请执行以下操作:Thread.CurrentPrincipal
必须具有访问机密对象所需的权限。ServiceHost
实例。ServiceBehavior
属性的InstanceContextMode
必须设置为InstanceContextMode.Single
。否则:
ServiceHost
实例。(可选)使用
FaultContract
属性修饰服务方法并从中抛出FaultException
以避免客户端通道出现故障。下面是一个示例:
服务配置文件:
客户端配置文件:
机密信息类:
服务合约及其实现 :
PrincipalPermission
attribute can adorn method or class. Therefore it is possible to restrict access to an instance of an object. Several things need to be done:Windows
as client credential type.PrincipalPermission
attribute.If singleton instance needs to be passed to
ServiceHost
constructor, do following:Thread.CurrentPrincipal
must have permissions necessary to access the confidential object.ServiceHost
instance by passing service singleton instance. PropertyInstanceContextMode
ofServiceBehavior
attribute must be set toInstanceContextMode.Single
.Otherwise:
ServiceHost
instance by passing the service type.Optionally, adorn the service method with
FaultContract
attribute and throwFaultException
from it in order to avoid faulting the client channel.Here is an example:
Service configuration file:
Client configuration file:
Confidential information class:
Service contract and its implementation:
如果您熟悉 .NET 权限编码(命令式或声明式),则该模式完全相同。在声明性形式中,PrincipalPermissionAttribute 应用于实现服务契约的类中的方法:
在此示例中,将检查当前主体以查看它是否属于名为 Updaters 的角色。在属性的实际实现中,调用主体上的 IsInRole 方法。
为了命令式确定PrincipalPermissionAttribute,将创建PrincipalPermission 类的实例。 PrimaryPermission 的构造函数将用户名和角色作为参数。实例化后,可以调用 Demand 方法来确定当前主体是否具有必要的权限。以下代码提供了一个示例:
配置应如下所示:
有关工作示例,请查看:授权访问服务操作
If you are familiar with .NET permission coding (either imperative or declarative), the pattern is exactly the same. In the declarative form, the PrincipalPermissionAttribute is applied to the method in the class that implements the service’s contract:
In this example, the current principal is checked to see whether it belongs to a role called Updaters. In the actual implementation of the attribute, the IsInRole method on the principal is called.
For imperative determination of the PrincipalPermissionAttribute, an instance of the PrincipalPermission class is created. The constructor for PrincipalPermission takes the username and role as a parameter. When instantiated, the Demand method can be called to determine whether the current principal has the necessary permissions. The following code provides an example:
the configuration should look like this:
for a working sample please look at: Authorizing Access to Service Operations