Sharepoint 修改域账户密码和本机用户密码

在sharepoint中没有提供修改密码的功能,估计也是因为不知道用户会使用哪种验证方式,AD or Form?

 

下面的用户使用的是AD验证,自己修改密码就成了很普通的事情,我们自己做一个webpart,写一些后台代码,

在欢迎哪里加一个菜单项,跳转到部署了修改密码webpart的页面就可以了。

 

#region 更改密码主要代码

        public string ChangeADUserPassword(string DomainName, string UserName, string oldPass, string newPass)
        // 用法:ChangeADUserPassword("AD", "VIRUS", "12345", "23456")
        {
            try
            {
                string strLDAP = "LDAP://" + DomainName;

                string fullLoginName = DomainName + "\\" + UserName;

                using (DirectoryEntry objDE = new DirectoryEntry(strLDAP, DomainName + "\\" + UserName, oldPass))
                {
                    DirectorySearcher deSearcher = new DirectorySearcher(objDE);
                    deSearcher.Filter = "(&(objectClass=user)(sAMAccountName=" + UserName + "))";
                    DirectoryEntry usr = deSearcher.FindOne().GetDirectoryEntry();
                    usr.Invoke("ChangePassword", new Object[2] { oldPass, newPass });
                    usr.CommitChanges();
                }
                return ("更改域用户密码,操作成功!");
            }
            catch (Exception ex)
            {
                return ("更改失败,错误信息:" + ex.Message);
            }

        }

        public static string ChangeLocalUserPassword(string LocalHostName, string UserName, string oldPass, string newPass, string AdminName, string AdminPass)
        {

            try
            {

                DirectoryEntry AD = new DirectoryEntry("WinNT://" + LocalHostName + ",computer");
                DirectoryEntry NewUser = AD.Children.Find(UserName);
                NewUser.Invoke("SetPassword", new object[] { newPass });
                NewUser.CommitChanges();

                return ("更改本机密码,操作成功!");

            }
            catch (Exception ex)
            {
                return ("更改失败,错误信息:" + ex.Message);
            }

        }

        #endregion

 

使用的时候就像下面一样,首先判断,机器名和域名是否相同,就知道是否在域中,然后决定用哪个修改密码的方法。

if (Environment.MachineName == Environment.UserDomainName)
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate() { lblErrorMessage.Text = ChangeLocalUserPassword(Environment.MachineName, lblMessage.Text.Substring(0, lblMessage.Text.IndexOf(" ")), txtOldPassword.Text, txtNewPassword.Text, "", ""); });
                }
                else
                {
                    lblErrorMessage.Text = ChangeADUserPassword(Environment.UserDomainName, Environment.UserName, txtOldPassword.Text, txtNewPassword.Text);
                }

你可能感兴趣的:(SharePoint)