C#获取AD用户信息

  根据一AD的帐号获另一个AD的帐号, 他们之间通过EmployeeID联系

 public class ADManager : IDisposable
    {
        DirectoryEntry sourceRoot, targetRoot;

        public ADManager(string sourcePath, string targetPath)
        {
            SourcePath = sourcePath;
            TargetPath = targetPath;
            sourceRoot = new DirectoryEntry(sourcePath.ToUpper());
            targetRoot = new DirectoryEntry(targetPath.ToUpper());
        }

        public string GetLoginID(string sAMAccountName)
        {
            string employeeID = GetEmployeeID(sAMAccountName, sourceRoot);
            string loginID = GetLoginID(employeeID, targetRoot);
            return loginID;
        }

        private string GetEmployeeID(string sAMAccountName, DirectoryEntry sourceRoot)
        {
            string employeeID = string.Empty;
            if (string.IsNullOrEmpty(sAMAccountName))
            {
                return employeeID;
            }

            using (DirectorySearcher searcher = new DirectorySearcher(sourceRoot, "(&(objectClass=user)(sAMAccountName=" + sAMAccountName + "))"))
            {
                searcher.PropertyNamesOnly = true;
                searcher.PageSize = 512;
                AddStandardUserPropertiesToSearch(searcher);
                SearchResult results = searcher.FindOne();
                if (results != null)
                {
                    using (DirectoryEntry user = results.GetDirectoryEntry())
                    {
                        employeeID = user.Properties["employeeID"][0].ToString();
                    }
                }// end results.Count >0

            }
            return employeeID;
        }

        private string GetLoginID(string employeeID, DirectoryEntry targetRoot)
        {
            string loginID = string.Empty;
            if (string.IsNullOrEmpty(employeeID))
            {
                return loginID;
            }
            using (DirectorySearcher searcher = new DirectorySearcher(targetRoot, "(&(objectClass=user)(employeeID=" + employeeID + "))"))
            {

                AddStandardUserPropertiesToSearch(searcher);
                //SearchResult results = searcher.FindOne();
                //if (results != null)
                //{
                //    using (DirectoryEntry user = results.GetDirectoryEntry())
                //    {
                //        employeeID = user.Properties["sAMAccountName"][0].ToString();
                //    }
                //}

                using (SearchResultCollection results = searcher.FindAll())
                {
                    List<string> logins = new List<string>();
                    foreach (SearchResult result in results)
                    {
                        using (DirectoryEntry user = result.GetDirectoryEntry())
                        {
                            string id = user.Properties["sAMAccountName"][0].ToString();
                            logins.Add(id);
                        }
                    }
                    if (logins.Count > 0)
                    {
                        loginID = logins[0];
                    }
                }
            }
           
            return loginID;
        }

        private static void AddStandardUserPropertiesToSearch(DirectorySearcher search)
        {
            search.PropertiesToLoad.Add("mail");
            search.PropertiesToLoad.Add("sAMAccountName");
            search.PropertiesToLoad.Add("employeeID");
            search.SearchScope = SearchScope.Subtree;
            search.PropertyNamesOnly = true;
            search.PageSize = 512;
        }
        public void Dispose()
        {
            if (sourceRoot != null)
                sourceRoot.Dispose();
            if (targetRoot != null)
                targetRoot.Dispose();
        }

        public string SourcePath { private set; get; }
        public string TargetPath { private set; get; }
    }


 public class PrincipalManager
    {

        PrincipalContext sourceContext, targetContext;

        public PrincipalManager(string sourceDomain, string targetDomain)
        {
            SourceDomain = sourceDomain;
            TargetDomain = targetDomain;
            sourceContext = new PrincipalContext(ContextType.Domain, sourceDomain.ToUpper());
            targetContext = new PrincipalContext(ContextType.Domain, targetDomain.ToUpper());
        }

        public string GetLoginID(string sAMAccountName)
        {
            string employeeID = GetEmployeeID(sAMAccountName, sourceContext);
            string loginID = GetLoginID(employeeID, targetContext);
            return loginID;
        }

        private string GetEmployeeID(string sAMAccountName, PrincipalContext sourceContext)
        {
            string employeeID = string.Empty;
            if (string.IsNullOrEmpty(sAMAccountName))
            {
                return employeeID;
            }

            using (UserPrincipal user = new UserPrincipal(sourceContext))
            {
                user.SamAccountName = sAMAccountName;
                using (PrincipalSearcher pS = new PrincipalSearcher(user))
                {
                    using (Principal results = pS.FindOne())
                    {
                        UserPrincipal princial = results as UserPrincipal;
                        if (princial != null)
                        {
                            employeeID = princial.EmployeeId;
                        }
                    }
                }
            }
            return employeeID;
        }

        private string GetLoginID(string employeeID, PrincipalContext targetContext)
        {
            string loginID = string.Empty;
            if (string.IsNullOrEmpty(employeeID))
            {
                return loginID;
            }

            using (UserPrincipal user = new UserPrincipal(targetContext))
            {
                user.EmployeeId=employeeID;
                using (PrincipalSearcher pS = new PrincipalSearcher(user))
                {
                    using (PrincipalSearchResult<Principal> results = pS.FindAll())
                    {
                        foreach (Principal item in results)
                        {
                            UserPrincipal princial = item as UserPrincipal;
                            if (princial != null)
                            {
                                loginID = princial.SamAccountName;
                            }
                        }
                     
                    }
                }
            }

            return loginID;
        }


        public void Dispose()
        {
            if (sourceContext != null)
                sourceContext.Dispose();
            if (targetContext != null)
                targetContext.Dispose();
        }

        public string SourceDomain { private set; get; }
        public string TargetDomain { private set; get; }
    }


http://blog.sina.com.cn/s/blog_683424be0100scuz.html
http://blog.csdn.net/shrenk/article/details/40991775

http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C#46

http://macaalay.com/2010/06/28/active-directory-c/

http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/all-operations-on-active-directory-ad-using-C-Sharp/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值