PrincipalPermission attribute can decorate a method or class. Therefore, you can restrict access to an instance of an object. There are a few things to do:
- Configure the selected service and client binding to use security. Specify
Windows as the client credential type. - Configure the service to use Windows groups for authorization.
- The adorn class that will contain sensitive information with the
PrincipalPermission attribute.
If you need to pass a singleton instance to the ServiceHost constructor, do the following:
- Create an instance of the Singleton service.
Thread.CurrentPrincipal must have the permissions necessary to access the confidential object. - Create an instance of
ServiceHost by passing an instance of the oneton service. The InstanceContextMode property of the ServiceBehavior attribute must be set to InstanceContextMode.Single .
Otherwise:
- Create an instance of
ServiceHost by passing the type of service.
Optionally, decorate the service method with the FaultContract attribute and throw a FaultException from it to avoid a client channel failure.
Here is an example:
Service configuration file :
<system.serviceModel> <services> <service name="Server.Service" behaviorConfiguration="Authorization"> <endpoint address="" binding="netTcpBinding" bindingConfiguration="TCP" contract="Common.IService" /> <host> <baseAddresses> <add baseAddress="net.tcp://localhost:13031/Service"/> </baseAddresses> </host> </service> </services> <bindings> <netTcpBinding> <binding name="TCP" openTimeout="00:30:00" closeTimeout="00:00:10" maxReceivedMessageSize="2147483647"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> <security mode="Message"> <message clientCredentialType="Windows" /> </security> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="Authorization"> <serviceAuthorization principalPermissionMode="UseWindowsGroups" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
Client configuration file :
<system.serviceModel> <client> <endpoint name="NetTcpBinding_IService" address="net.tcp://localhost:13031/Service" binding="netTcpBinding" bindingConfiguration="TCP" contract="Common.IService" /> </client> <bindings> <netTcpBinding> <binding name="TCP" openTimeout="00:30:00" closeTimeout="00:00:10" sendTimeout="00:30:00" receiveTimeout="00:30:00" maxReceivedMessageSize="2147483647"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> <security mode="Message"> <message clientCredentialType="Windows" /> </security> </binding> </netTcpBinding> </bindings> </system.serviceModel>
Confidential Information Class :
[PrincipalPermission(SecurityAction.Demand, Role = "Administrators" ) ] public class ContactInfo { public string FirstName { get; set; } public string LastName { get; set; } public ContactInfo() { FirstName = "John"; LastName = "Doe"; } public override string ToString() { return string.Format( "{0} {1}", FirstName, LastName ); } }
Service contract and its implementation :
[ServiceContract] public interface IService { [OperationContract] [FaultContract( typeof( string ) )] string GetName( int id ); } [ServiceBehavior]
Rest Wing Feb 27 2018-11-22T00: 00Z
source share