예제 #1
0
        /// <summary>
        /// 创建域用户,"administrator","Ccc2008neu","administrator","Ccc2008neu"
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="displayName"></param>
        /// <param name="description"></param>
        /// <param name="pwd"></param>
        public static bool AddUser(string loginName, string displayName, string email, string phone, string pwd, string topPath, string groupName, string schoolName, bool enabled)
        {
            string ouPath = AddOU(topPath, schoolName);
            bool   result;
            string content = "";
            //先加安全组,帐号重复会出错;否则会出现错误
            DirectoryEntry grp = AddGroup(new DirectoryEntry(topPath), groupName);

            using (DirectoryEntry AD = new DirectoryEntry(ouPath))
            {
                try
                {
                    using (DirectoryEntry NewUser = AD.Children.Add("CN=" + loginName, "user"))
                    {
                        NewUser.Properties["displayName"].Add(displayName);
                        NewUser.Properties["name"].Add(displayName);
                        NewUser.Properties["sAMAccountName"].Add(loginName);
                        NewUser.Properties["userPrincipalName"].Add(loginName + DomainName);
                        if (phone != "")
                        {
                            NewUser.Properties["telephoneNumber"].Add(phone);
                        }
                        if (email != "")
                        {
                            NewUser.Properties["mail"].Add(email);
                        }
                        NewUser.CommitChanges();
                        try
                        {
                            ActiveDs.IADsUser user = (ActiveDs.IADsUser)NewUser.NativeObject;
                            user.AccountDisabled = !enabled;
                            user.SetPassword(pwd);
                            //密码永不过期
                            dynamic flag = user.Get("userAccountControl");

                            int newFlag = 0X10000;
                            user.Put("userAccountControl", newFlag);
                            user.SetInfo();

                            NewUser.CommitChanges();
                        }
                        catch (Exception ex)
                        {
                            content += ex.ToString() + "\r\f";
                        }
                        if (groupName != "")
                        {
                            AddUserToGroup(grp, NewUser);
                        }
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    content += ex.ToString();
                    result   = false;
                }
            }
            return(result);
        }
예제 #2
0
        public bool AddUser(string path, UserInfo userInfo)
        {
            bool isResult = false;

            using (DirectoryEntry AD = new DirectoryEntry(path))
            {
                using (DirectoryEntry NewUser = AD.Children.Add("CN=" + userInfo.name, "user"))
                {
                    NewUser.Properties["displayName"].Add(userInfo.displayName);
                    NewUser.Properties["name"].Add(userInfo.name);
                    NewUser.Properties["sAMAccountName"].Add(userInfo.sAMAccountName);
                    NewUser.Properties["userPrincipalName"].Add(userInfo.userPrincipalName);
                    NewUser.Properties["description"].Add(userInfo.description);
                    NewUser.CommitChanges();

                    ActiveDs.IADsUser user = (ActiveDs.IADsUser)NewUser.NativeObject;
                    user.AccountDisabled = !userInfo.userEnabled;
                    user.SetPassword(userInfo.userPassword);
                    //密码永不过期
                    dynamic flag = user.Get("userAccountControl");

                    int newFlag = 0X10000;
                    user.Put("userAccountControl", newFlag);
                    user.SetInfo();

                    NewUser.CommitChanges();

                    isResult = true;
                }
            }
            return(isResult);
        }