示例#1
0
        public void AddMemberToLocalGroup(string accountName, string groupName)
        {
            Trace.Entering();
            LocalGroupMemberInfo memberInfo = new LocalGroupMemberInfo();

            memberInfo.FullName = accountName;

            int returnCode = NetLocalGroupAddMembers(null,              // computer name
                                                     groupName,
                                                     3,                 // group info with fullname (vs sid)
                                                     ref memberInfo,
                                                     1);                //total entries

            // return on success
            if (returnCode == ReturnCode.S_OK)
            {
                Trace.Info($"Account '{accountName}' is added to local group '{groupName}'.");
                return;
            }

            // Error Cases
            switch (returnCode)
            {
            case ReturnCode.ERROR_MEMBER_IN_ALIAS:
                Trace.Info(StringUtil.Format("Account {0} is already member of group {1}", accountName, groupName));
                break;

            case ReturnCode.NERR_GroupNotFound:
            case ReturnCode.ERROR_NO_SUCH_ALIAS:
                throw new ArgumentException(StringUtil.Loc("GroupDoesNotExists", groupName));

            case ReturnCode.ERROR_NO_SUCH_MEMBER:
                throw new ArgumentException(StringUtil.Loc("MemberDoesNotExists", accountName));

            case ReturnCode.ERROR_INVALID_MEMBER:
                throw new ArgumentException(StringUtil.Loc("InvalidMember"));

            case ReturnCode.ERROR_ACCESS_DENIED:
                throw new UnauthorizedAccessException(StringUtil.Loc("AccessDenied"));

            default:
                throw new Exception(StringUtil.Loc("OperationFailed", nameof(NetLocalGroupAddMembers), returnCode));
            }
        }
 private extern static int NetLocalGroupAddMembers([MarshalAs(UnmanagedType.LPWStr)] string serverName,
                                                   [MarshalAs(UnmanagedType.LPWStr)] string groupName,
                                                   int level,
                                                   ref LocalGroupMemberInfo buf,
                                                   int totalEntries);
 private extern static int NetLocalGroupAddMembers([MarshalAs(UnmanagedType.LPWStr)] string serverName,
                                                  [MarshalAs(UnmanagedType.LPWStr)] string groupName,
                                                  int level,
                                                  ref LocalGroupMemberInfo buf,
                                                  int totalEntries);
        public void AddMemberToLocalGroup(string accountName, string groupName)
        {
            Trace.Entering();
            LocalGroupMemberInfo memberInfo = new LocalGroupMemberInfo();
            memberInfo.FullName = accountName;

            int returnCode = NetLocalGroupAddMembers(null, groupName, 3, ref memberInfo, 1);

            // return on success
            if (returnCode == ReturnCode.S_OK)
            {
                return;
            }

            // Error Cases
            switch (returnCode)
            {
                case ReturnCode.ERROR_MEMBER_IN_ALIAS:
                    Trace.Info(StringUtil.Format("Account {0} is already member of group {1}", accountName, groupName));
                    break;
                case ReturnCode.NERR_GroupNotFound:
                case ReturnCode.ERROR_NO_SUCH_ALIAS:
                    throw new ArgumentException(StringUtil.Loc("GroupDoesNotExists", groupName));

                case ReturnCode.ERROR_NO_SUCH_MEMBER:
                    throw new ArgumentException(StringUtil.Loc("MemberDoesNotExists", accountName));

                case ReturnCode.ERROR_INVALID_MEMBER:
                    throw new ArgumentException(StringUtil.Loc("InvalidMember"));

                case ReturnCode.ERROR_ACCESS_DENIED:
                    throw new UnauthorizedAccessException(StringUtil.Loc("AccessDenied"));

                default:
                    throw new Exception(StringUtil.Loc("OperationFailed", nameof(NetLocalGroupAddMembers), returnCode));
            }
        }