C#对域访问的相关方法
日期:2018/6/28  发布人:润宇软件  浏览量:22
 

/*------------------------------------------------------------------------------------
*Model Name:   DomainOperate
*Effect:       DomainOperate,Get Domain User and Get Domain Infomation
*Author:       lingliu
*Create time:  2007.7.26
*--------------------------Modify Infomation record----------------------------------
* Time         Operater      Description
*-------------------------------------------------------------------------------------
*2007.7.26     lingliu       create
*-----------------------------------------------------------------------------------*/
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
namespace Utility
{
    public class DomainOperate
    {
        #region GetDomainList
        /// <summary>
        /// GetDomainList
        /// </summary>
        /// <returns>Domain Talbe</returns>
        public static DataTable GetDomainList()
        {
            //Set DirectoryEntry info
            DirectoryEntry root = new DirectoryEntry("WinNT:");         
            DirectoryEntries domains = root.Children;
            domains.SchemaFilter.Add("domain");
            //Create Datatable
            DataTable dt = new DataTable("DomainList");
            DataColumn dc = new DataColumn("DomainName");
            dt.Columns.Add(dc);
            DataRow mdr = dt.NewRow();
            mdr["DomainName"] = "Please select domain";
            dt.Rows.Add(mdr);
            foreach (DirectoryEntry domain in domains)
            {
                DataRow dr = dt.NewRow();
                dr["DomainName"] = domain.Name;
                dt.Rows.Add(dr);
            }
            return dt;
        }
        #endregion
        #region  Get All Object By Domain
        /// <summary>
        /// Get All Object By Domain
        /// </summary>
        /// <param name="domain">Domain name</param>
        /// <param name="userName">Name of Domain</param>
        /// <param name="password">Password of Domain</param>
        /// <param name="objectClass">Objece Class name:  For Example:user,computer,domain eg.</param>
        /// <returns>Objece Table</returns>
        public static DataTable GetAllObjectByDomain(string domain, string userName, string password, string objectClass,ref string rtnMsg)
        {
            //Create DataTalbe
            DataTable dt = new DataTable("DomainObject");
            DataColumn dc = new DataColumn("ObjectName");
            dt.Columns.Add(dc);
            //Set DirectoryEntry Info
            DirectoryEntry entry = new DirectoryEntry("LDAP://"+ domain +".com");
            entry.Password = password;//password of domain admin
            entry.Username = userName;//username of domain admin
            DirectorySearcher mySearcher = new DirectorySearcher(entry);
            mySearcher.Filter = ("(objectClass=" + objectClass + ")");//filter data
           
            //Get Object Class Name
            try
            {
                foreach (SearchResult resEnt in mySearcher.FindAll())
                {
                    DataRow dr = dt.NewRow();
                    dr["ObjectName"] = resEnt.GetDirectoryEntry().Name.ToString().Remove(0, 3);
                    dt.Rows.Add(dr);
                }
            }
            catch(Exception ex)
            {
                rtnMsg = ex.Message.ToString();
                dt.Clear();
            }
            return dt;
        }
        #endregion
    }
}