System.Security.Principal.WindowsPrincipal.IsInRole C# (CSharp) Method

IsInRole() public method

public IsInRole ( System.Security.Principal.SecurityIdentifier sid ) : bool
sid System.Security.Principal.SecurityIdentifier
return bool
        public virtual bool IsInRole(SecurityIdentifier sid)
        {
            if (sid == null)
                throw new ArgumentNullException(nameof(sid));
            Contract.EndContractBlock();

            // special case the anonymous identity.
            if (_identity.AccessToken.IsInvalid)
                return false;

            // CheckTokenMembership expects an impersonation token
            SafeAccessTokenHandle token = SafeAccessTokenHandle.InvalidHandle;
            if (_identity.ImpersonationLevel == TokenImpersonationLevel.None)
            {
                if (!Interop.Advapi32.DuplicateTokenEx(_identity.AccessToken,
                                                  (uint)TokenAccessLevels.Query,
                                                  IntPtr.Zero,
                                                  (uint)TokenImpersonationLevel.Identification,
                                                  (uint)TokenType.TokenImpersonation,
                                                  ref token))
                    throw new SecurityException(new Win32Exception().Message);
            }

            bool isMember = false;
            // CheckTokenMembership will check if the SID is both present and enabled in the access token.
            if (!Interop.Advapi32.CheckTokenMembership((_identity.ImpersonationLevel != TokenImpersonationLevel.None ? _identity.AccessToken : token),
                                                  sid.BinaryForm,
                                                  ref isMember))
                throw new SecurityException(new Win32Exception().Message);

            token.Dispose();
            return isMember;
        }

Same methods

WindowsPrincipal::IsInRole ( WindowsBuiltInRole role ) : bool
WindowsPrincipal::IsInRole ( int rid ) : bool
WindowsPrincipal::IsInRole ( string role ) : bool

Usage Example

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

            //Create a windowsidentity object representing the current user
            WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();

            //Create a windowsprincipal object representing the current user
            WindowsPrincipal currentprincipal = new WindowsPrincipal(currentIdentity);

            //Set the security policy context to windows security
            System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

            //hide the subtract and multiply button if user is not and Administrator
            if(!currentprincipal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                subtractButton.Visible = false;
                multiplyButton.Visible = false;
            }

            //hide the add button if user is not in Users group
            if(!currentprincipal.IsInRole(WindowsBuiltInRole.User))
            {
                addButton.Visible = false;
            }

            //Hide the Divide button if the user is not named CPhilips
            if(!(currentIdentity.Name.ToLower() == Environment.MachineName.ToLower() + @"\rafa&pri"))
            {
                divideButton.Visible = false;
            }
		}
All Usage Examples Of System.Security.Principal.WindowsPrincipal::IsInRole