Example #1
0
        public void SetPrivilege(PrivilegeName privilegeName, PrivilegeAttribute privilegeAttribute)
        {
            TokPriv1Luid tokenPrivilege;
            IntPtr       hCurrentProcess = Kernel32.GetCurrentProcess();
            IntPtr       hProcessToken   = IntPtr.Zero;

            if (!AdvApi32.OpenProcessToken(hCurrentProcess, AdvApi32.TOKEN_ADJUST_PRIVILEGES | AdvApi32.TOKEN_QUERY, ref hProcessToken))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            tokenPrivilege.Count = 1;
            tokenPrivilege.Luid  = 0;
            tokenPrivilege.Attr  = (int)privilegeAttribute;

            if (!AdvApi32.LookupPrivilegeValue(null, PrivilegeStrings[(int)privilegeName], ref tokenPrivilege.Luid))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            if (!AdvApi32.AdjustTokenPrivileges(hProcessToken, false, ref tokenPrivilege, 0, IntPtr.Zero, IntPtr.Zero))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Example #2
0
        public static Privileges GetPrivileges <TEnum>()
        {
            FieldInfo[] fields = typeof(TEnum).GetFields();
            if (fields.Length == 1)
            {
                return(null);
            }
            Privileges privilege = new Privileges();

            FieldInfo[] fieldInfoArray = fields;
            for (int i = 0; i < fieldInfoArray.Length; i++)
            {
                object[] customAttributes = fieldInfoArray[i].GetCustomAttributes(typeof(PrivilegeAttribute), true);
                if (customAttributes.Length != 0)
                {
                    GroupActionItem           groupActionItem     = new GroupActionItem();
                    ActionItem                actionItem          = new ActionItem();
                    List <string>             strs                = new List <string>();
                    List <PrivilegeAttribute> privilegeAttributes = new List <PrivilegeAttribute>();
                    List <Controllers>        controllers         = new List <Controllers>();
                    object[] objArray = customAttributes;
                    for (int j = 0; j < objArray.Length; j++)
                    {
                        object             obj                = objArray[j];
                        Controllers        controller         = new Controllers();
                        PrivilegeAttribute privilegeAttribute = obj as PrivilegeAttribute;
                        controller.ControllerName = privilegeAttribute.Controller;
                        List <string> actionNames = controller.ActionNames;
                        string        action      = privilegeAttribute.Action;
                        char[]        chrArray    = new char[] { ',' };
                        actionNames.AddRange(action.Split(chrArray));
                        controllers.Add(controller);
                        privilegeAttributes.Add(privilegeAttribute);
                    }
                    PrivilegeAttribute privilegeAttribute1 = privilegeAttributes.FirstOrDefault((PrivilegeAttribute a) => !string.IsNullOrEmpty(a.GroupName));
                    groupActionItem.GroupName = privilegeAttribute1.GroupName;
                    actionItem.PrivilegeId    = privilegeAttribute1.Pid;
                    actionItem.Name           = privilegeAttribute1.Name;
                    actionItem.Url            = privilegeAttribute1.Url;
                    actionItem.Controllers.AddRange(controllers);
                    GroupActionItem groupActionItem1 = privilege.Privilege.FirstOrDefault((GroupActionItem a) => a.GroupName == groupActionItem.GroupName);
                    if (groupActionItem1 != null)
                    {
                        groupActionItem1.Items.Add(actionItem);
                    }
                    else
                    {
                        groupActionItem.Items.Add(actionItem);
                        privilege.Privilege.Add(groupActionItem);
                    }
                }
            }
            return(privilege);
        }
Example #3
0
        public static Privileges GetPrivileges <TEnum>()
        {
            Func <PrivilegeAttribute, bool> func = null;
            Type typeFromHandle = typeof(TEnum);

            FieldInfo[] fields = typeFromHandle.GetFields();
            Privileges  result;

            if (fields.Length == 1)
            {
                result = null;
            }
            else
            {
                Privileges  privileges = new Privileges();
                FieldInfo[] array      = fields;
                for (int i = 0; i < array.Length; i++)
                {
                    FieldInfo fieldInfo        = array[i];
                    object[]  customAttributes = fieldInfo.GetCustomAttributes(typeof(PrivilegeAttribute), true);
                    if (customAttributes.Length != 0)
                    {
                        GroupActionItem           group      = new GroupActionItem();
                        ActionItem                actionItem = new ActionItem();
                        List <string>             list       = new List <string>();
                        List <PrivilegeAttribute> list2      = new List <PrivilegeAttribute>();
                        List <Controllers>        list3      = new List <Controllers>();
                        object[] array2 = customAttributes;
                        for (int j = 0; j < array2.Length; j++)
                        {
                            object             obj                = array2[j];
                            Controllers        controllers        = new Controllers();
                            PrivilegeAttribute privilegeAttribute = obj as PrivilegeAttribute;
                            controllers.ControllerName = privilegeAttribute.Controller;
                            controllers.ActionNames.AddRange(privilegeAttribute.Action.Split(new char[]
                            {
                                ','
                            }));
                            list3.Add(controllers);
                            list2.Add(privilegeAttribute);
                        }
                        IEnumerable <PrivilegeAttribute> arg_13A_0 = list2;
                        if (func == null)
                        {
                            func = ((PrivilegeAttribute a) => !string.IsNullOrEmpty(a.GroupName));
                        }
                        PrivilegeAttribute privilegeAttribute2 = arg_13A_0.FirstOrDefault(func);
                        group.GroupName        = privilegeAttribute2.GroupName;
                        actionItem.PrivilegeId = privilegeAttribute2.Pid;
                        actionItem.Name        = privilegeAttribute2.Name;
                        actionItem.Url         = privilegeAttribute2.Url;
                        actionItem.Controllers.AddRange(list3);
                        GroupActionItem groupActionItem = privileges.Privilege.FirstOrDefault((GroupActionItem a) => a.GroupName == group.GroupName);
                        if (groupActionItem == null)
                        {
                            group.Items.Add(actionItem);
                            privileges.Privilege.Add(group);
                        }
                        else
                        {
                            groupActionItem.Items.Add(actionItem);
                        }
                    }
                }
                result = privileges;
            }
            return(result);
        }
Example #4
0
        } // end constructor

        public TokenPrivilege(LUID_AND_ATTRIBUTES laa)
        {
            Privilege  = _GetPrivilegeFromLuid(laa.Luid, out m_name);
            Attributes = (PrivilegeAttribute)laa.Attributes;
        } // end constructor
Example #5
0
 public TokenPrivilege(Privilege privilege, PrivilegeAttribute attributes)
 {
     Privilege  = privilege;
     Attributes = attributes;
 } // end constructor
Example #6
0
 public TokenPrivilegeEntry(string name, PrivilegeAttribute attributes)
 {
     Name       = name ?? throw new ArgumentNullException(nameof(name));
     Attributes = attributes;
 }
Example #7
0
 public TokenPrivilegeEntry(string name, PrivilegeAttribute attributes)
 {
     Name       = name;
     Attributes = attributes;
 }