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
PrincipalPermissionattribute can adorn method or class. Therefore it is possible to restrict access to an instance of an object. Several things need to be done:Windowsas client credential type.PrincipalPermissionattribute.If singleton instance needs to be passed to
ServiceHostconstructor, do following:Thread.CurrentPrincipalmust have permissions necessary to access the confidential object.ServiceHostinstance by passing service singleton instance. PropertyInstanceContextModeofServiceBehaviorattribute must be set toInstanceContextMode.Single.Otherwise:
ServiceHostinstance by passing the service type.Optionally, adorn the service method with
FaultContractattribute and throwFaultExceptionfrom 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: