Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4005752
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:25:55+00:00 2026-05-20T08:25:55+00:00

how to use [PrincipalPermission(SecurityAction.Demand, Role = Administrators)] attribute on a class? I am looking

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:25:55+00:00Added an answer on May 20, 2026 at 8:25 am

    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:

    1. Configure selected service and client binding to use security. Specify Windows as client credential type.
    2. Configure service to use Windows groups for authorization.
    3. Adorn class that will contain confidential information with PrincipalPermission attribute.

    If singleton instance needs to be passed to ServiceHost constructor, do following:

    1. Create service singleton instance. Thread.CurrentPrincipal must have permissions necessary to access the confidential object.
    2. Create ServiceHost instance by passing service singleton instance. Property InstanceContextMode of ServiceBehavior attribute must be set to InstanceContextMode.Single.

    Otherwise:

    1. Create ServiceHost instance by passing the service type.

    Optionally, adorn the service method with FaultContract attribute and throw FaultException from it in order to avoid faulting the client channel.

    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]
    // Use following if singleton instance needs to be passed to `ServiceHost` constructor
    //[ServiceBehavior( InstanceContextMode = InstanceContextMode.Single )]
    public class Service : IService
    {
        private Dictionary<int, ContactInfo> Contacts { get; set; }
        public Service()
        {
            Contacts = new Dictionary<int, ContactInfo>();
            IPrincipal originalPrincipal = Thread.CurrentPrincipal;
            try
            {
                Thread.CurrentPrincipal = new WindowsPrincipal( WindowsIdentity.GetCurrent() );
                Contacts.Add( 1, new ContactInfo() );
            }
            finally
            {
                Thread.CurrentPrincipal = originalPrincipal;
            }
        }
        public string GetName( int id )
        {
            if ( Contacts.Count < id )
                return null;
            try
            {
                return Contacts[ id ].ToString();
            }
            catch ( Exception ex )
            {
                throw new FaultException<string>( ex.Message );
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple method that is secured [PrincipalPermission(SecurityAction.Demand, Role = Administrator)] protected void
Use case: 3rd party application wants to programatically monitor a text file being generated
I use rsync to synchronize files to Windows clients in a server agnostic way.
I use the VS2008 command prompt for builds, TFS access etc. and the cygwin
I use a stream reader to import some data and at the moment I
Q1 - I’m not sure I understand why we should prefer to use PrincipalPermission.Union()
I've created the following attribute: [Serializable] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited =
USE [Fk_Test2] GO /****** Object: Table [dbo].[Owners] Script Date: 08/20/2010 16:52:44 ******/ SET ANSI_NULLS
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Use case: A does something on his box and gots stuck. He asks B

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.