欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#读取ActiveDirectory

程序员文章站 2022-07-09 19:05:38
public class ActiveDirectoryManager { public static DirectoryEntry GetDirectoryEntry() { DirectoryEntry entry = null; try { if (entry == null) { entry ......
 public class activedirectorymanager
    {
        public static directoryentry getdirectoryentry()
        {
            directoryentry entry = null;
            try
            {
                if (entry == null)
                {
                    entry = new directoryentry(activedirectoryhelper.adstring, activedirectoryhelper.adusername, activedirectoryhelper.adpassword, authenticationtypes.secure);
                }
                return entry;
            }
            catch (exception ex)
            {

                throw ex;
            }
        }

        public static directorysearcher getdirectorysearcher(directoryentry entry)
        {
            directorysearcher searcher = null;
            try
            {
                if (searcher == null)
                {
                    searcher = new directorysearcher(entry)
                    {
                        cacheresults = true,
                        searchscope = searchscope.subtree
                    };
                    var propertys = activedirectoryhelper.adproperty.split(new char[] { ',' }, stringsplitoptions.removeemptyentries);
                    searcher.propertiestoload.clear();
                    foreach (var item in propertys)
                    {
                        searcher.propertiestoload.add(item);
                    }
                }
                return searcher;
            }
            catch (exception ex)
            {
                throw ex;
            }
        }

    }

    public class activedirectoryhelper
    {
        public static string adstring = configurationmanager.connectionstrings["adconnectionstring"].connectionstring;

        public static string adusername = configurationmanager.appsettings["adusername"];

        public static string adpassword = configurationmanager.appsettings["adpassword"];

        public static string adproperty = configurationmanager.appsettings["adproperty"];

        public static string adkey = configurationmanager.appsettings["adkey"];

        public static string aduserwhere = configurationmanager.appsettings["aduserwhere"].replace("﹠", "&");

        public static list<object> getdirectoryinfo(directorysearcher searcher, string username = null, string usercode = null)
        {
            var where = string.empty;
            if (!string.isnullorwhitespace(username) || !string.isnullorwhitespace(usercode))
            {
                where = string.format(aduserwhere, username == null ? string.empty : username, usercode == null ? string.empty : usercode);
            }
            else
            {
                where = "(&(objectclass=user))";
            }
                
            return executedatasource(searcher, where);
        }

        private static list<object> executedatasource(directorysearcher searcher, string where)
        {
            if (!string.isnullorwhitespace(where)) searcher.filter = where;

            searchresultcollection result = searcher.findall();
            func<searchresult, bool> basicwhere = x => true;
            var keys = adkey.split(new char[] { ',' }, stringsplitoptions.removeemptyentries);
            foreach (var item in keys)
            {
                basicwhere += x => !string.isnullorwhitespace(x.properties.get<string>(item));
            }

            //return result.oftype<searchresult>().where(x => !resultorempty(x)).select(x => getdynamic(x)).tolist();
            //var searchresults = result.oftype<searchresult>().where(basicwhere);
            var propertys = adproperty.split(new char[] { ',' }, stringsplitoptions.removeemptyentries);
            return result.oftype<searchresult>().select(x => getdynamic(x, propertys)).where(x=>x != null).tolist();
        }

        private static object getdynamic(searchresult result,string[] propertys)
        {
            dynamic info = new expandoobject();

            var dic = (idictionary<string, object>)info;

            //var propertys = adproperty.split(new char[] { ',' }, stringsplitoptions.removeemptyentries);
            var keys = adkey.split(new char[] { ',' }, stringsplitoptions.removeemptyentries);
            foreach (var item in propertys)
            {
                if (string.isnullorwhitespace(result.properties.get<string>(item)) && keys.contains(item))
                {
                    return null;
                }
                dic.add(item, result.properties.get<string>(item));
            }
            return info;
        }

        private static bool resultorempty(searchresult result)
        {
            bool isempty = false;

            var keys = adkey.split(new char[] { ',' }, stringsplitoptions.removeemptyentries);

            foreach (var item in keys)
            {
                if (string.isnullorwhitespace(result.properties.get<string>(item)))
                {
                    isempty = true;
                    break;
                }

            }
            return isempty;
        }

    }

    public static class resultpropertycollectionextension
    {
        public static t get<t>(this resultpropertycollection target, string propertyname)
        {
            if (target == null)
                throw new argumentnullexception("target");

            if (string.isnullorempty(propertyname))
                throw new argumentnullexception("propertyname");

            if (target.contains(propertyname) && target[propertyname].count > 0)
                return (t)target[propertyname][0];
            return default(t);
        }
    }