コード例 #1
0
        /// <summary>
        /// Deletes a group
        /// </summary>
        /// <param name="groupName"></param>
        public static void DeleteGroup(string groupName)
        {
            int returnCode = Win32GroupInterop.NetLocalGroupDel(null, groupName);

            switch (returnCode)
            {
            case Win32GroupInterop.ReturnCode.S_OK:
            case Win32GroupInterop.ReturnCode.ERROR_NO_SUCH_ALIAS:
            case Win32GroupInterop.ReturnCode.NERR_GroupNotFound:
                break;

            default:
                throw new Exception(string.Format("DeleteGroup failed: {0}", returnCode));
            }
        }
コード例 #2
0
        /// <summary>
        /// Removes a user from the named group
        /// </summary>
        /// <param name="groupName"></param>
        /// <param name="userName"></param>
        public static void RemoveMemberFromGroup(string groupName, string userName)
        {
            Win32GroupInterop.LocalGroupMemberInfo memberInfo = new Win32GroupInterop.LocalGroupMemberInfo();
            memberInfo.FullName = userName;

            int returnCode = Win32GroupInterop.NetLocalGroupDelMembers(null, groupName, 3, ref memberInfo, 1);

            switch (returnCode)
            {
            case Win32GroupInterop.ReturnCode.S_OK:
            case Win32GroupInterop.ReturnCode.ERROR_MEMBER_NOT_IN_ALIAS:
                break;

            default:
                throw new Exception(string.Format("RemoveMemberFromGroup failed: {0}", returnCode));
            }
        }
コード例 #3
0
        /// <summary>
        /// Creates a group
        /// </summary>
        /// <param name="groupName"></param>
        /// <param name="groupComment"></param>
        public static void CreateGroup(string groupName, string groupComment)
        {
            Win32GroupInterop.LocalGroupInfo groupInfo = new Win32GroupInterop.LocalGroupInfo();
            groupInfo.Name    = groupName;
            groupInfo.Comment = groupComment;

            int returnCode = Win32GroupInterop.NetLocalGroupAdd(null, 1, ref groupInfo, 0);

            switch (returnCode)
            {
            case Win32GroupInterop.ReturnCode.S_OK:
            case Win32GroupInterop.ReturnCode.ERROR_ALIAS_EXISTS:
            case Win32GroupInterop.ReturnCode.NERR_GroupExists:
                break;

            default:
                throw new Exception(string.Format("CreateGroup failed: {0}", returnCode));
            }
        }
コード例 #4
0
        public static int TryGetSidsOfLocalGroupMembers(string serverName, string localGroupName, out List <SecurityIdentifier> sids)
        {
            if (string.IsNullOrEmpty(localGroupName))
            {
                throw new ArgumentNullException("localGroupName");
            }

            sids = new List <SecurityIdentifier>();

            // Note: serverName can be (and usually is) null.
            uint   entriesRead;
            uint   totalEntries;
            IntPtr resumeHandle = IntPtr.Zero;
            IntPtr bufPtr       = IntPtr.Zero;

            try
            {
                int returnCode = Win32GroupInterop.NetLocalGroupGetMembers(serverName,
                                                                           localGroupName,
                                                                           0,             // level 0. return the security identifier (SID) associated with the local group member. The bufptr parameter points to an array of LOCALGROUP_MEMBERS_INFO_0 structures
                                                                           out bufPtr,
                                                                           uint.MaxValue, // maximum preferred length. The method MUST allocate as much space as the data requires.
                                                                           out entriesRead,
                                                                           out totalEntries,
                                                                           out resumeHandle);

                if (returnCode != Win32GroupInterop.ReturnCode.S_OK)
                {
                    return(returnCode);
                    //if (returnCode == Win32GroupInterop.ReturnCode.ERROR_ACCESS_DENIED)
                    //{
                    //    //throw new UnauthorizedAccessException(AdminResources.AccessDenied());
                    //}
                    //else if (returnCode == Win32GroupInterop.ReturnCode.NERR_GroupNotFound ||
                    //         returnCode == Win32GroupInterop.ReturnCode.ERROR_NO_SUCH_ALIAS)
                    //{
                    //    //throw new ArgumentException(AdminResources.GroupNotExist(), "localGroupName");
                    //}

                    ////throw new ConfigurationException(AdminResources.ErrorOperationWithReturnCode("NetLocalGroupGetMembers", returnCode.ToString(CultureInfo.CurrentCulture)));
                }

                for (int index = 0; index < entriesRead; ++index)
                {
                    IntPtr ptr = new IntPtr((long)bufPtr + Marshal.SizeOf(typeof(Win32GroupInterop.LocalGroupMemberInfo0)) * index);

                    Win32GroupInterop.LocalGroupMemberInfo0 groupMemberInfo =
                        (Win32GroupInterop.LocalGroupMemberInfo0)Marshal.PtrToStructure(ptr, typeof(Win32GroupInterop.LocalGroupMemberInfo0));

                    SecurityIdentifier sid = new SecurityIdentifier(groupMemberInfo.Sid);

                    sids.Add(sid);
                }

                return(Win32GroupInterop.ReturnCode.S_OK);
            }
            finally
            {
                if (bufPtr != IntPtr.Zero)
                {
                    int rc = Win32GroupInterop.NetApiBufferFree(bufPtr);

                    if (rc != Win32GroupInterop.ReturnCode.S_OK)
                    {
                        TraceManager.TraceError("Failed to free buffer returned by NetLocalGroupGetMembers(). Error: {0}", rc);
                    }
                }
            }
        }