Beispiel #1
0
        /// <summary>
        /// 获取指定域内所有用户账号信息      Gets all user account information in the specified domain
        /// </summary>
        /// <param name="url">域路径 Domain path</param>
        /// <returns></returns>
        public List <DomainUser> getAllUser(string url)
        {
            List <DomainUser> user = new List <DomainUser>();
            DirectoryEntry    ent  = new DirectoryEntry(pathHead + url + pathFoot, adminUser, adminPwd);

            //循环子节点取值    Value of cyclic sub node
            foreach (DirectoryEntry child in ent.Children)
            {
                DomainUser domainuser = new DomainUser();
                string[]   sArray     = child.Name.Split('=');
                domainuser.UserName = sArray[1];
                domainuser.type     = child.SchemaClassName;
                if (child.Properties["userAccountControl"].Value != null)
                {
                    //判断账号是否是正常启用状态     Determine whether the account is normally enabled
                    if (child.Properties["userAccountControl"].Value.ToString() == "66048" || child.Properties["userAccountControl"].Value.ToString() == "512")
                    {
                        domainuser.state = true;
                    }
                    else
                    {
                        domainuser.state = false;
                    }
                }
                else
                {
                    domainuser.state = false;
                }
                if (child.Properties["description"].Value != null)
                {
                    //获取账号描述信息      Get account description information
                    domainuser.Description = child.Properties["description"].Value.ToString();
                }
                else
                {
                    domainuser.Description = "";
                }
                user.Add(domainuser);
            }
            ent.Close();
            return(user);
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string name = context.Request.Form["name"];
            string url  = context.Request.Form["url"];

            string domainip   = context.Session["domainip"].ToString();
            string domainname = context.Session["domainname"].ToString();
            string username   = context.Session["username"].ToString();
            string password   = context.Session["password"].ToString();
            string dc         = context.Session["dc"].ToString();

            Operate op = new Operate(domainname, domainip, username, password, dc);

            DomainUser user = new DomainUser();

            user = op.getuserinfo(url, name);
            JObject json = JObject.FromObject(user);

            context.Response.Write(json);
        }
Beispiel #3
0
        /// <summary>
        /// 查询详细用户信息    Query user details
        /// </summary>
        /// <param name="url">账户路径 Account path </param>
        /// <param name="name">账户名 Account name</param>
        /// <returns></returns>
        public DomainUser getuserinfo(string url, string name)
        {
            DomainUser user = new DomainUser();

            try
            {
                DirectoryEntry ent      = new DirectoryEntry(pathHead + url + pathFoot, adminUser, adminPwd);
                DirectoryEntry UserInfo = ent.Children.Find("CN=" + name);
                if (UserInfo.Properties["givenName"].Value != null)
                {
                    user.firstName = UserInfo.Properties["givenName"].Value.ToString();
                }
                else
                {
                    user.firstName = "";
                }

                if (UserInfo.Properties["sn"].Value != null)
                {
                    user.lastName = UserInfo.Properties["sn"].Value.ToString();
                }
                else
                {
                    user.lastName = "";
                }
                user.sAMAccountname    = UserInfo.Properties["sAMAccountname"].Value.ToString();
                user.UserPrincipalName = UserInfo.Properties["userPrincipalName"].Value.ToString();
                if (UserInfo.Properties["displayName"].Value != null)
                {
                    user.displayName = UserInfo.Properties["displayName"].Value.ToString();
                }
                else
                {
                    user.displayName = "";
                }
                if (UserInfo.Properties["description"].Value != null)
                {
                    user.Description = UserInfo.Properties["description"].Value.ToString();
                }
                else
                {
                    user.Description = "";
                }
                if (UserInfo.Properties["department"].Value != null)
                {
                    user.Department = UserInfo.Properties["department"].Value.ToString();
                }
                else
                {
                    user.Department = "";
                }
                if (UserInfo.Properties["telephoneNumber"].Value != null)
                {
                    user.Telephone = UserInfo.Properties["telephoneNumber"].Value.ToString();
                }
                else
                {
                    user.Telephone = "";
                }

                return(user);
            }
            catch (DirectoryServicesCOMException E)
            {
                user.Message = E.Message.ToString();
                return(user);
            }
        }
Beispiel #4
0
        /// <summary>
        /// 修改更新用户信息    Update user's information
        /// </summary>
        /// <param name="url">用户路径 user path</param>
        /// <param name="olduser">原用户名 old user name</param>
        /// <param name="user">用户对象 DomainUser object</param>
        /// <returns></returns>

        public string update(string url, string olduser, DomainUser user)
        {
            try
            {
                DirectoryEntry ent     = new DirectoryEntry(pathHead + url + pathFoot, adminUser, adminPwd);
                DirectoryEntry newUser = ent.Children.Find("CN=" + olduser);
                //AD域只允许把空改为非空,不能将非空改为空
                //Can't change the empty information to non empty
                if (user.lastName != "" && user.lastName != null)
                {
                    newUser.Properties["sn"].Value = user.lastName;
                }
                if (user.firstName != "" && user.firstName != null)
                {
                    newUser.Properties["givenName"].Value = user.firstName;
                }
                if (user.sAMAccountname != "" && user.sAMAccountname != null)
                {
                    newUser.Properties["sAMAccountname"].Value = user.sAMAccountname;
                }
                if (user.UserPrincipalName != "" && user.UserPrincipalName != null)
                {
                    newUser.Properties["userPrincipalName"].Value = user.UserPrincipalName;
                }
                if (user.Description != "" && user.Description != null)
                {
                    newUser.Properties["description"].Value = user.Description;
                }
                if (user.displayName != "" && user.displayName != null)
                {
                    newUser.Properties["displayName"].Value = user.displayName;
                }
                if (user.Department != "" && user.Department != null)
                {
                    newUser.Properties["department"].Value = user.Department;
                }
                if (user.Telephone != "" && user.Telephone != null)
                {
                    newUser.Properties["telephoneNumber"].Value = user.Telephone;
                }
                newUser.CommitChanges();

                //修改用户名     Update user name(use it if you want)
                //if (user.UserName!=""&&user.UserName!=olduser)
                //{
                //    newUser.Rename("CN=" + user.UserName);
                //    newUser.CommitChanges();
                //}


                //修改密码      Change password
                if (user.UserPwd != null && user.NewPwd != null & user.UserPwd != "" && user.NewPwd != "")
                {
                    try
                    {
                        newUser.Invoke("ChangePassword", new object[] { user.UserPwd, user.NewPwd });
                        newUser.CommitChanges();
                    }
                    catch (Exception ex)
                    {
                        string    message       = "";
                        Exception baseException = ex.GetBaseException();
                        if (baseException is COMException)
                        {
                            COMException comException = baseException as COMException;
                            switch (comException.ErrorCode)
                            {
                            case -2147024810:
                                message = "The original password error!";
                                break;

                            case -2147022651:
                                message = "Indicates that the password does not conform to the domain security requirements!";
                                break;

                            case -2147023570:
                                message = "Invalid password!";
                                break;

                            case -2147016657:
                                message = "User password is invalid!";
                                break;

                            default:
                                message = "Unknown error!";
                                break;
                            }
                            return(message);
                        }
                    }
                }

                ent.Close();
                newUser.Close();
                return("Update information success!");
            }
            catch (DirectoryServicesCOMException E)
            {
                return("Update information! fail!" + E.Message.ToString());
            }
        }
Beispiel #5
0
        /// <summary>
        /// 新建账户   Add user account
        /// </summary>
        /// <param name="url">新建账户所在路径 The path to the new account</param>
        /// <param name="user">用户对象   DomainUser object</param>
        /// <returns></returns>

        public string adduser(string url, DomainUser user)
        {
            string result = string.Empty;

            try
            {
                user.UserName = user.lastName + user.firstName;
                DirectoryEntry ent     = new DirectoryEntry(pathHead + url + pathFoot, adminUser, adminPwd);
                DirectoryEntry newUser = ent.Children.Add("CN=" + user.UserName, "user");
                if (user.lastName != "")
                {
                    //设置姓       Set lastname
                    newUser.Properties["sn"].Value = user.lastName;
                }
                if (user.firstName != "")
                {
                    //设置名       Set firstname
                    newUser.Properties["givenName"].Value = user.firstName;
                }
                //设置登录账号名   Set login name
                newUser.Properties["sAMAccountname"].Value = user.sAMAccountname;
                //设置登录账号名   Set login name
                newUser.Properties["userPrincipalName"].Value = user.UserPrincipalName;
                if (user.displayName != "" && user.displayName != null)
                {
                    //设置显示名    Set display name
                    newUser.Properties["displayName"].Value = user.displayName;
                }
                if (user.Department != "" && user.Department != null)
                {
                    //设置部门      Set department
                    newUser.Properties["department"].Value = user.Department;
                }
                if (user.Description != "" && user.Description != null)
                {
                    //设置描述      Set description
                    newUser.Properties["description"].Value = user.Description;
                }
                if (user.Telephone != "" && user.Telephone != null)
                {
                    //设置电话号码    Set telephone number
                    newUser.Properties["telephoneNumber"].Value = user.Telephone;
                }
                newUser.CommitChanges();
                //设置密码          Set password
                newUser.Invoke("SetPassword", new object[] { user.UserPwd });
                //启用账号          Enable account
                newUser.Properties["userAccountControl"].Value = 66048;
                newUser.CommitChanges();

                ent.Close();
                newUser.Close();
                result = "Add new user success!";
                return(result);
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                result = "Add new user error!" + E.Message.ToString();
                return(result);
            }
        }