Greg
Greg

Reputation: 11478

Protected Constructor Derived Class Issue

I'm sure this question has been asked quite a few times; however I'm running into a problem. So I've created a separate class; specifically to verify that the proper user level is present.

Below is the code to test those permission levels:

    class Elevated_Rights
    {

        // Token Bool:
        private bool _level = false;

        #region Constructor:

        protected Elevated_Rights()
        {

            // Invoke Method On Creation:
            Elevate();

        }

        #endregion

        public void Elevate()
        {

            // Get Identity:
            WindowsIdentity user = WindowsIdentity.GetCurrent();

            // Set Principal
            WindowsPrincipal role = new WindowsPrincipal(user);

            #region Test Operating System for UAC:

            if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6)
            {

                // False:
                _level = false;

                // Todo: Exception/ Exception Log

            }

            #endregion

            else
            {

                #region Test Identity Not Null:

                if (user == null)
                {

                    // False:
                    _level = false;

                    // Todo: "Exception Log / Exception"

                }

                #endregion

                else
                {

                    #region Ensure Security Role:

                    if (!(role.IsInRole(WindowsBuiltInRole.Administrator)))
                    {

                        // False:
                        _level = false;

                        // Todo: "Exception Log / Exception"

                    }

                    else
                    {

                        // True:
                        _level = true;

                    }

                    #endregion


                } // Nested Else 'Close'

            } // Initial Else 'Close'

        } // End of Class.

    }

So that part is working as intended; however when I inherit this class into another class to utilize the protected constructor is where I hit a snag.

    class Default_Configuration : Elevated_Rights
    {

        #region Constructor:

        public Default_Configuration() : base()
        {


            Elevate();


        }

        #endregion



    }

But When I call that new class; the method states: "Invalid Access due to Constructor Permission". It theoretically should work; is there something I'm missing? Any help would be greatly appreciated.

Upvotes: 3

Views: 1209

Answers (2)

Naresh
Naresh

Reputation: 678

        new Default_Configuration().Elevate();

this line works for me.

What is not working for you?

Upvotes: 0

Carson63000
Carson63000

Reputation: 4232

I think your problem lies elsewhere: I pasted those two class definitions into a project, builds just fine. Instantiated a new Default_Configuration, called Elevate(), no errors.

If you have other issues with mixing public and protected methods, here's a blog post by Peter Hallam that describes the issues.

Basically, you can't call a protected instance method in a base class from a public instance method in a derived class; doing so would allow a "malicious" derived class from making all the protected methods in the base class public, just by writing wrappers for them.

Upvotes: 2

Related Questions