控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

ASP.NET访问域用户(AD活动目录)信息的类

作者:佚名 出处:互联网 2011年11月22日 阅读:

ASP.NET访问域用户(AD活动目录)信息的类

public static class DomainInformation
    {
               
        #region Constants
        //static string[] usersLdapPath = @"LDAP://zzzzzz.com/OU=xxxxxx,DC=yyyyyy,DC=com";
        static string usersLdapPath = System.Configuration.ConfigurationManager.AppSettings["LDAPConnectionString"].ToString() ;
        const string adLoginName = "administrator"; //管理员用户
        const string adLoginPassword = "88888888";
        #endregion
       
        static public string[] GetGroupsForUser(string domainADsPath, string username)// 获取用户所属组
        {
            DirectoryEntry usersDE = Directoryunits(domainADsPath);
            DirectorySearcher ds = new DirectorySearcher(usersDE);
            ds.Filter = "(&(sAMAccountName=" + username + "))";
            ds.PropertiesToLoad.Add("memberof");
            SearchResult r = ds.FindOne();
            if (r.Properties["memberof"].Count == 0)
            {
                return (null);
            }
            string[] results = new string[r.Properties["memberof"].Count];
            for (int i = 0; i < r.Properties["memberof"].Count; i++)
            {
                string theGroupPath = r.Properties["memberof"][i].ToString();
                results[i] = theGroupPath.Substring(3, theGroupPath.IndexOf(",") - 3);
            }
            usersDE.Close();
            return (results);
        }
        /// <summary>
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static string[] GetGroupsForUser(string username)
        {
            DirectoryEntry usersDE = DomainInformation.Directory();
            DirectorySearcher ds = new DirectorySearcher(usersDE);
            ds.Filter = "(&(sAMAccountName=" + username + "))";
            ds.PropertiesToLoad.Add("memberof");
            SearchResult r = ds.FindOne();
            if (r.Properties["memberof"] == null)
            {
                return (null);
            }
            string[] results = new string[r.Properties["memberof"].Count+1];
            for (int i = 0; i < r.Properties["memberof"].Count; i++)
            {
                string theGroupPath = r.Properties["memberof"][i].ToString();
                results[i] = theGroupPath.Substring(3, theGroupPath.IndexOf(",") - 3);
            }
            results[r.Properties["memberof"].Count]="All";//All组属于任何人,在AD之外定义了一个组,以便分配用户权限
            usersDE.Close();
            return (results);
        }
        static public string[] GetUsersForGroup(string domainADsPath, string Groupname)// 获取用户
        {
            DirectoryEntry usersDE = Directoryunits(domainADsPath);
            DirectorySearcher ds = new DirectorySearcher(usersDE);
            ds.Filter = "(&(objectClass=group)(cn=" + Groupname + "))";
            ds.PropertiesToLoad.Add("member");
            SearchResult r = ds.FindOne();
            if (r.Properties["member"] == null)
            {
                return (null);
            }
            string[] results = new string[r.Properties["member"].Count];
            for (int i = 0; i < r.Properties["member"].Count; i++)
            {
                string theGroupPath = r.Properties["member"][i].ToString();
                results[i] = theGroupPath.Substring(3, theGroupPath.IndexOf(",") - 3);
            }
            usersDE.Close();
            return (results);
        }
        static public string GetUserDisplayName(string username)// 获取组用户
        {
            string results;
            DirectoryEntry usersDE = Directory();
           
            DirectorySearcher ds = new DirectorySearcher(usersDE);
            ds.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";
            ds.PropertiesToLoad.Add(UserProperty.DisplayName);
            SearchResult r = ds.FindOne();
            results = r.GetDirectoryEntry().InvokeGet(UserProperty.DisplayName).ToString();
            usersDE.Close();
            return (results);
          
        }
        static public UserInfoEx GetUserInfoEx(string username)      //获取域用户详细信息
        {
            DirectoryEntry usersDE =Directory();
            DirectorySearcher ds = new DirectorySearcher(usersDE);
            ds.Filter = "(&(objectClass=user)(objectCatogery=person)(sAMAccountName=" + username + "))";
            ds.PropertiesToLoad.Add("cn");
            ds.PropertiesToLoad.Add(UserProperty.Name);
            ds.PropertiesToLoad.Add(UserProperty.UserName);
            ds.PropertiesToLoad.Add(UserProperty.homePhone);
            ds.PropertiesToLoad.Add(UserProperty.FirstName);
            ds.PropertiesToLoad.Add(UserProperty.LastName);
            ds.PropertiesToLoad.Add(UserProperty.Email);
            ds.PropertiesToLoad.Add(UserProperty.Title);
            ds.PropertiesToLoad.Add(UserProperty.Company);
            ds.PropertiesToLoad.Add(UserProperty.Address);
            ds.PropertiesToLoad.Add(UserProperty.City);
            ds.PropertiesToLoad.Add(UserProperty.State);
            ds.PropertiesToLoad.Add(UserProperty.PostalCode);
            ds.PropertiesToLoad.Add(UserProperty.Phone);
            ds.PropertiesToLoad.Add(UserProperty.Country);
            SearchResult r = ds.FindOne();
            UserInfoEx result = new UserInfoEx();
         
            result.Name = r.GetDirectoryEntry().InvokeGet(UserProperty.Name).ToString();
            result.LoginName = r.GetDirectoryEntry().InvokeGet(UserProperty.UserName).ToString();
            if (r.GetDirectoryEntry().InvokeGet(UserProperty.FirstName) != null)
            {
                result.FirstName = r.GetDirectoryEntry().InvokeGet(UserProperty.FirstName).ToString();
            }
            else
            {
                result.FirstName = "";
            }
            if (r.GetDirectoryEntry().InvokeGet(UserProperty.homePhone) != null)
            {
                result.homePhone = r.GetDirectoryEntry().InvokeGet(UserProperty.homePhone).ToString();
            }
            else
            {
                result.homePhone = "";
            }
            if (r.GetDirectoryEntry().InvokeGet(UserProperty.LastName)!= null)
            {
                result.LastName = r.GetDirectoryEntry().InvokeGet(UserProperty.LastName).ToString();
            }
            else
            {
                result.LastName = "";
            }
            if (r.GetDirectoryEntry().InvokeGet(UserProperty.Email) != null)
            {
                result.EmailAddress = r.GetDirectoryEntry().InvokeGet(UserProperty.Email).ToString();
            }
            else
            {
                result.EmailAddress = "";
            }
            if (r.GetDirectoryEntry().InvokeGet(UserProperty.Title) != null)
            {
                result.Title = r.GetDirectoryEntry().InvokeGet(UserProperty.Title).ToString();
            }
            else
            {
                result.Title = "";
            }
            if (r.GetDirectoryEntry().InvokeGet(UserProperty.Company) != null)
            {
                result.Company =r.GetDirectoryEntry().InvokeGet(UserProperty.Company).ToString();
            }
            else
            {
                result.Company = "";
            }
            if (r.GetDirectoryEntry().InvokeGet(UserProperty.Address) != null)
            {
                result.Address =r.GetDirectoryEntry().InvokeGet(UserProperty.Address).ToString();
            }
            else
            {
                result.Address = "";
            }
            if (r.GetDirectoryEntry().InvokeGet(UserProperty.City) != null)
            {
                result.City =r.GetDirectoryEntry().InvokeGet(UserProperty.City).ToString();
            }
            else
            {
                result.City = "";
            }
            if (r.GetDirectoryEntry().InvokeGet(UserProperty.State) != null)
            {
                result.State =r.GetDirectoryEntry().InvokeGet(UserProperty.State).ToString();
            }
            else
            {
                result.State = "";
            }
            if (r.GetDirectoryEntry().InvokeGet(UserProperty.PostalCode) != null)
            {
                result.PostalCode =r.GetDirectoryEntry().InvokeGet(UserProperty.PostalCode).ToString();
            }
            else
            {
                result.PostalCode = "";
            }
            if (r.GetDirectoryEntry().InvokeGet(UserProperty.Phone) != null)
            {
                result.Phone = r.GetDirectoryEntry().InvokeGet(UserProperty.Phone).ToString();
            }
            else
            {
                result.Phone = "";
            }
            if (r.GetDirectoryEntry().InvokeGet(UserProperty.Country) != null)
            {
                result.Country =r.GetDirectoryEntry().InvokeGet(UserProperty.Country).ToString();
            }
            else
            {
                result.Country = "";
            }
            usersDE.Close();
            return (result);
        }
        static private string GetAdGroupDescription(string prefix)//根据CN获取组description
        {
            string results;
            DirectoryEntry groupsDE = Directory();
            DirectorySearcher groupsDS = new DirectorySearcher(groupsDE);
            groupsDS.Filter = "(&(objectClass=group)(CN=" + prefix + "*))";
            groupsDS.PropertiesToLoad.Add("cn");
            SearchResult sr = groupsDS.FindOne();
            results = sr.GetDirectoryEntry().InvokeGet("description").ToString();
            groupsDE.Close();
            return (results);
        }
        static private DataTable GetAdGroupInfo()//根据CN获取组信息
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("URL", typeof(System.String));
            dt.Columns.Add("cn", typeof(System.String));
            dt.Columns.Add("Description", typeof(System.String));
            DirectoryEntry groupsDE = Directory();
            DirectorySearcher searcher = new DirectorySearcher(groupsDE);
          
            searcher.Filter = "(&(objectClass=group))";
            //searcher.SearchScope = SearchScope.Subtree;
            //searcher.Sort = new SortOption("description", System.DirectoryServices.SortDirection.Ascending);
            searcher.PropertiesToLoad.AddRange(new string[] { "cn", "description"});
            SearchResultCollection results = searcher.FindAll();
            if (results.Count == 0)
            {
                return (null);
            }
            else {
                foreach (SearchResult result in results)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = result.Path.ToString();
                    dr[1] = result.GetDirectoryEntry().InvokeGet("cn").ToString();
                    if (result.GetDirectoryEntry().InvokeGet("Description")!=null)
                    dr[2] = result.GetDirectoryEntry().InvokeGet("Description").ToString();
                    else
                    dr[2] = result.GetDirectoryEntry().InvokeGet("cn").ToString();
                    dt.Rows.Add(dr);
                }
                dt.DefaultView.Sort = "description ASC";
                groupsDE.Close();
                return dt;
           
            }
        }
        static public string getAccountName(string cn) //根据CN获取登陆名
        {
            foreach (string path in usersLdapPath)
            {
                DirectoryEntry userContainerDE = Directoryunits(path);
                DirectorySearcher ds = new DirectorySearcher(userContainerDE);
                ds.Filter = "(&(objectClass=user)(cn=*" + cn + "*))";
                ds.PropertiesToLoad.Add("sAMAccountName");
                SearchResult r = ds.FindOne();
                if (r!=null)
                return r.GetDirectoryEntry().InvokeGet("sAMAccountName").ToString();
            }
            return null;
        }
        static public bool isAdUser(string username)//判断是否域用户
        {
            DirectoryEntry userContainerDE = Directory();
            DirectorySearcher ds = new DirectorySearcher(userContainerDE);
            ds.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";
            ds.PropertiesToLoad.Add("cn");
            SearchResult r = ds.FindOne();
            if (r == null)
            {
                userContainerDE.Close();
                return false;
               
            }
            else
            {
                userContainerDE.Close();
                return true;
            }
        }
 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统