예제 #1
0
        /// <summary>
        /// 添加用户登录到
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="computerName"></param>
        /// <returns></returns>
        public static bool UpdateUserWorkStation(string loginName, string computerName, UserWorkStationOperType type)
        {
            LdapEntry entry = GetUser(loginName);

            if (entry == null)
            {
                throw new Exception($"名为:{loginName} 的用户在AD中不存在");
            }

            List <string> stations = entry.AttrStringValue("userWorkstations").Split(',').ToList();

            if (type == UserWorkStationOperType.Add && !stations.Contains(computerName))
            {
                stations.Add(computerName);
            }
            else if (type == UserWorkStationOperType.Remove && stations.Contains(computerName))
            {
                stations.Remove(computerName);
            }

            LdapAttribute attributePassword = new LdapAttribute("userWorkstations", string.Join(',', stations));

            _connection.Modify(entry.DN, new LdapModification(LdapModification.REPLACE, attributePassword));
            return(true);
        }
예제 #2
0
        /// <summary>
        /// 移动用户到新的OU
        /// </summary>
        /// <param name="loginName">登录名</param>
        /// <param name="rcn">如果要修改cn,可以指定新的值,否则传原始值</param>
        /// <param name="ouContainer"></param>
        /// <returns></returns>
        public static bool MoveUserToOU(string loginName, string rcn = "", string ouContainer = "")
        {
            LdapEntry entry = GetUser(loginName);

            if (entry == null)
            {
                throw new Exception($"名为:{loginName} 的用户在AD中不存在");
            }

            string cn = entry.AttrStringValue("cn");

            cn = rcn == "" ? cn : rcn;
            string newRCN = $"CN={cn}";

            if (string.IsNullOrWhiteSpace(ouContainer))
            {
                _connection.Rename(entry.DN, newRCN, true);
            }
            else
            {
                _connection.Rename(entry.DN, newRCN, ouContainer, true);
            }

            return(true);
        }