zoukankan      html  css  js  c++  java
  • .net操作AD域

    2011-06-14 16:22

     #region 启用账户
            /// <summary>
            /// 启用账户
            /// </summary>
            /// <param name="user"></param>
            public bool EnableAccount(string userName)
            {
                try
                {
                    DirectoryEntry userEntry = FindObject("user", userName);
                    int val = (int)userEntry.Properties["userAccountControl"].Value;
                    userEntry.Properties["userAccountControl"].Value = val & ~0x2;
                    userEntry.CommitChanges();
                    userEntry.Close();
                    //DomainUser._success = "启用账户成功!";
                    return true;
                }
                catch (Exception ex)
                {
                    //DomainUser._failed = ex.Message.ToString();
                    return false;
                }
            }
            #endregion

            #region 停用账号
            /// <summary>
            /// 停用账号
            /// </summary>
            /// <param name="user"></param>
            public  bool DisableAccount(string userName)
            {
                try
                {
                    DirectoryEntry userEntry = FindObject("user", userName);
                    userEntry.Properties["userAccountControl"].Value = 0x2;
                    userEntry.CommitChanges();
                    userEntry.Close();
                    //DomainUser._success = "停用账户成功!";
                    return true;

                }
                catch (System.DirectoryServices.DirectoryServicesCOMException ex)
                {
                    //DomainUser._failed = ex.Message.ToString();
                    return false;
                }
            }
            #endregion

            #region 判断用户是否已经存在域中
            /// <summary>
            /// 判断用户是否已经存在域中
            /// </summary>
            /// <param name="userName">用户名</param>
            /// <returns></returns>
            private DirectoryEntry ExitUser(string userName)
            {
                try
                {
                    DirectoryEntry de = null;
                    de = FindObject("user", userName);
                    if (de == null)
                    {
                        return new DirectoryEntry(); ;
                    }
                    else
                    {
                        return de;
                    }
                }
                catch (Exception ex)
                {
                    //DomainUser._failed = ex.Message.ToString();
                    return new DirectoryEntry();
                }
            }
            #endregion

            #region 判断域中是否存在组
            /// <summary>
            /// 判断域中是否存在组
            /// </summary>
            /// <param name="groupName">组名</param>
            /// <returns></returns>lan
            private DirectoryEntry ExitGroup(string groupName, string groupPath)
            {
                DirectoryEntry rootUser = null;
                DirectoryEntry group = null;
                try
                {
                    string path = GetOrganizeNamePath(groupPath);
                    rootUser = new DirectoryEntry(path, adminUser, adminPwd, AuthenticationTypes.Secure);
                    group = rootUser.Children.Find("CN=" + groupName);
                    if (group != null)
                    {
                        return group;
                    }
                    return new DirectoryEntry();
                }
                catch (Exception ex)
                {
                   // DomainUser._failed = ex.Message.ToString() + "在域中不存在组“" + groupName + "”或路组织单位不正确";
                    return new DirectoryEntry();
                }
            }
            #endregion

            #region 判断域中是否存在组织单位
            /// <summary>
            /// 判断域中是否存在组织单位
            /// </summary>
            /// <param name="organizeName">组织单位名</param>
            /// <returns></returns>
            private bool ExitOU(string organizeName)
            {
                DirectoryEntry rootUser = null;
                DirectoryEntry ouFind = null;
                if (string.IsNullOrEmpty(organizeName))
                {
                    return true;
                }
                else 
                {
                    //分解路径
                    string[] allOu = organizeName.Split(new char[] { '/' });
                    //获取直属部门
                    string OUName = allOu[allOu.Length - 1].ToString();
                    try
                    {
                        string path = GetOrganizeNamePath(organizeName);
                        rootUser = new DirectoryEntry(path, adminUser, adminPwd, AuthenticationTypes.Secure);
                        ouFind = rootUser.Parent.Children.Find("OU=" + OUName);
                        if (ouFind != null)
                        {
                            return true;
                        }
                        return false;
                    }
                    catch (Exception ex)
                    {
                        //DomainUser._failed = ex.Message.ToString() + "在域中不存在组织单位“" + OUName + "”";
                        return false;
                    }
                }
            }
            #endregion

            #region 获取域用户信息
            /// <summary>
            /// 获取域用户信息
            /// </summary>
            /// <param name="path">目录</param>
            /// <param name="username">用户名</param>
            /// <returns></returns>
            public DomainUser GetAdUserInfo(string userName)
            {
                DomainUser du = new DomainUser();
                DirectoryEntry de = FindObject("user", userName);
                if (de != null)
                {
                    if (de.Properties["samAccountName"].Value != null)
                    {
                        du.UserId = de.Properties["samAccountName"].Value.ToString();
                    }
                    if (de.Properties["displayName"].Value != null)
                    {
                        du.UserName = de.Properties["displayName"].Value.ToString();
                    }
                    if (de.Properties["userPrincipalName"].Value != null)
                    {
                        du.UserPrincipalName = de.Properties["userPrincipalName"].Value.ToString();
                    }
                    if (de.Properties["telephoneNumber"].Value != null)
                    {
                        du.Telephone = de.Properties["telephoneNumber"].Value.ToString();
                    }
                    if (de.Properties["mail"].Value != null)
                    {
                        du.Email = de.Properties["mail"].Value.ToString();
                    }
                    if (de.Properties["description"].Value != null)
                    {
                        du.Description = de.Properties["description"].Value.ToString();
                    }
                    if (de.Properties["Department"].Value != null)
                    {
                        du.Department = de.Properties["Department"].Value.ToString();
                    }
                }
                return du;
            }
            #endregion

            #region 从域中按照用户名查找用户
            /// <summary>
            /// 从域中按照用户名查找用户
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="AdUser">管理员账户</param>
            /// <param name="AdPwd">管理员密码</param>
            /// <param name="username">用户名</param>
            /// <returns></returns>
            private DirectoryEntry GetUser(string path, string username)
            {

                DirectoryEntry deuser;
                try
                {
                    DirectoryEntry de = new DirectoryEntry(path, adminUser, adminPwd);
                    DirectorySearcher deSearch = new DirectorySearcher(de);
                    deSearch.Filter = "(&(objectClass=user)(cn=" + username + "))";
                    deSearch.SearchScope = SearchScope.Subtree;
                    SearchResult result = deSearch.FindOne();
                    if (result != null)
                    {
                        deuser = result.GetDirectoryEntry();
                        return deuser;
                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception ex)
                {
                    //DomainUser._failed = ex.Message.ToString();
                    return null;
                }
            }
            #endregion

            #region 进入AD域查询
            /// <summary>
            /// 查寻用户信息
            /// </summary>
            /// <param name="userName">用户名</param>
            private List<string> AccsesADQuery(string userName)
            {
                //定义de进入AD架构
                DirectoryEntry de = new DirectoryEntry(GetDomainPath(), adminUser, adminPwd);
                //定义ds查找AD
                DirectorySearcher ds = new DirectorySearcher(de);
                string value = string.Empty;
                List<string> domainList = new List<string>();
                try
                {
                    //3.定义查询
                    ds.Filter = "(SAMAccountName=" + userName + ")";
                    ds.PropertiesToLoad.Add("SAMAccountName");//account
                    ds.PropertiesToLoad.Add("Name");//full name
                    ds.PropertiesToLoad.Add("displayName");
                    ds.PropertiesToLoad.Add("mail");
                    ds.PropertiesToLoad.Add("sn");
                    ds.PropertiesToLoad.Add("description");
                    ds.PropertiesToLoad.Add("Department");
                    ds.PropertiesToLoad.Add("userPrincipalName");//user logon name,xxx@bdxy.com
                    ds.PropertiesToLoad.Add("physicalDeliveryOfficeName");
                    ds.PropertiesToLoad.Add("telephoneNumber");
                    //查找一个
                    SearchResult sr = ds.FindOne();
                    if (sr != null)
                    {
                        //列出值
                        foreach (string key in sr.Properties.PropertyNames)
                        {
                            foreach (object obj in de.Properties[key])
                            {
                                value += key + " = " + obj + Environment.NewLine;
                                domainList.Add(value);
                            }
                        }
                        return domainList;
                    }
                    else
                    {
                        return domainList;
                    }
                }
                catch (Exception ex)
                {
                    //DomainUser._failed = ex.Message.ToString();
                    return domainList;
                }
                finally
                {
                    if (ds != null)
                    {
                        ds.Dispose();
                    }
                    if (de != null)
                    {
                        de.Dispose();
                    }

  • 相关阅读:
    正负样本不平衡的处理
    基于贝叶斯优化的超参数搜索
    Axure RP的基础使用
    keil:C语言里面调用汇编程序
    keil的使用:新建Project
    汇编入门:循环语句的汇编实现
    转gif图
    LeetCode题解 #5 Longest Palindromic Substring
    Android 从本地图库或拍照后裁剪图片并设置头像
    Android使用简单的Service
  • 原文地址:https://www.cnblogs.com/lilyzhang/p/2416974.html
Copyright © 2011-2022 走看看