zoukankan      html  css  js  c++  java
  • C#修改AD账号及密码

    在使用AD域环境搭建的账号系统修改密码的时候比较麻烦一般需要管理员在域环境去进行对用户的密码进行修改。

    以下就是用来查询和修改AD域密码的方法。

     1         /// <summary>
     2         /// AD账户查询
     3         /// </summary>
     4         /// <param name="Domain"></param>
     5         /// <param name="UserAccount"></param>
     6         /// <param name="UserPassWord"></param>
     7         /// <returns></returns>
     8         private static string CheckAccount(string Domain, string UserAccount, string UserPassWord)
     9         {
    10             string ReturnValue = string.Empty;
    11             try
    12             {
    13                 //throw new InvalidPluginExecutionException(Domain + "|==|" + UserAccount + "|==|" + UserPassWord);
    14                 DirectoryEntry entry = new DirectoryEntry("LDAP://" + Domain, UserAccount, UserPassWord, AuthenticationTypes.Secure);
    15                 DirectorySearcher mySearcher = new DirectorySearcher(entry);
    16                 mySearcher.Filter = ("(&(objectClass=user)(sAMAccountName=" + UserAccount + "))");
    17                 SearchResult searchResult = mySearcher.FindOne();
    18                 if (searchResult != null)
    19                 {
    20                     ReturnValue = "1";
    21                 }
    22             }
    23             catch (Exception ex)
    24             {
    25                 ReturnValue = "账号有误:" + ex.Message;
    26             }
    27             return ReturnValue;
    28         }
    View Code
    /// <summary>
            /// 修改AD域密码
            /// </summary>
            /// <param name="Domain"></param>
            /// <param name="AdministratorAccount">管理员账号</param>
            /// <param name="UserAccount">待修改密码账户</param>
            /// <param name="AdministratorPassWord">管理员账户密码</param>
            /// <param name="UserNewPassWord">新用户新密码</param>
            /// <param name="User_ConfirmPassword">新用户新密码确认</param>
            /// <returns></returns>
            private static string UpdateADAccount(string Domain, string AdministratorAccount, string UserAccount, string AdministratorPassWord, string UserNewPassWord, string User_ConfirmPassword)
            {
                //反馈内容、
                string ReturnString = string.Empty;
                if (UserNewPassWord == User_ConfirmPassword)
                {
                    try
                    {
                        DirectoryEntry entry = new DirectoryEntry("LDAP://" + Domain, AdministratorAccount, AdministratorPassWord, AuthenticationTypes.Secure);
                        DirectorySearcher mySearcher = new DirectorySearcher(entry);
                        mySearcher.Filter = ("(&(objectClass=user)(sAMAccountName=" + UserAccount + "))");
                        SearchResult searchResult = mySearcher.FindOne();
                        DirectoryEntry userEntry = searchResult.GetDirectoryEntry();
                        userEntry.Invoke("SetPassword", new object[] { UserNewPassWord });
                        ReturnString = "修改密码成功!!";
                    }
                    catch (Exception ex)
                    {
                        ReturnString = "修改密码出错!!问题可能是:" + ex.Message;
                    }
                }
                else
                {
                    ReturnString = "两次密码输入不一致";
                }
    
                return ReturnString;
            }
    View Code
  • 相关阅读:
    LInux 安全测试 2
    LInux 安全测试
    又一款linux提权辅助工具
    shell 生成指定范围随机数与随机字符串 .
    腾讯新浪通过IP地址获取当前地理位置(省份)的接口
    建站指南:百度认为什么样的网站更有抓取和收录价值2012-06-20
    ngx_lua 金山项目 黄雀
    在页面中使用js
    多线程取读文件(别人的,有点晕,先放在这里有时间研究研究)
    factory工厂模式
  • 原文地址:https://www.cnblogs.com/bzpfly/p/13577110.html
Copyright © 2011-2022 走看看