C# 获取域用户信息

 

C#读取域信息,需要引用两个程序集

 

using System;
using System.Collections.Generic;
using System.Data;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;

namespace BaseClass.Common
{
    public class ActiveDirectory
    {

        /// <summary>
        /// 获取域中的用户,并返回datatable
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAdUser()
        {
            IPGlobalProperties ipGlobal = IPGlobalProperties.GetIPGlobalProperties();
            string domainName = ipGlobal.DomainName;

            PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domainName);
            UserPrincipal userPrincipal = new UserPrincipal(principalContext);
            PrincipalSearcher principalSearcher = new PrincipalSearcher(userPrincipal);

            DataTable dtUser = new DataTable();
            dtUser.Columns.Add("Sid", typeof(string));
            dtUser.Columns.Add("SamAccountName", typeof(string));
            dtUser.Columns.Add("UserPrincipalName", typeof(string));
            dtUser.Columns.Add("Surname", typeof(string));
            dtUser.Columns.Add("MiddleName", typeof(string));
            dtUser.Columns.Add("GivenName", typeof(string));
            dtUser.Columns.Add("Name", typeof(string));
            dtUser.Columns.Add("Description", typeof(string));
            dtUser.Columns.Add("EmailAddress", typeof(string));
            dtUser.Columns.Add("LastLogon", typeof(string));

            foreach (UserPrincipal userPrincipalSearchResult in principalSearcher.FindAll())
            {
                DataRow row = dtUser.NewRow();
                dtUser.Rows.Add(row);

                row["Sid"] = userPrincipalSearchResult.Sid;
                row["SamAccountName"] = userPrincipalSearchResult.SamAccountName;
                row["UserPrincipalName"] = userPrincipalSearchResult.UserPrincipalName;
                row["Surname"] = userPrincipalSearchResult.Surname;
                row["MiddleName"] = userPrincipalSearchResult.MiddleName;
                row["GivenName"] = userPrincipalSearchResult.GivenName;
                row["Name"] = userPrincipalSearchResult.Name;
                row["Description"] = userPrincipalSearchResult.Description;
                row["EmailAddress"] = userPrincipalSearchResult.EmailAddress;
                row["LastLogon"] = userPrincipalSearchResult.LastLogon;
            }
            userPrincipal.Dispose();

            return dtUser;
        }

        /// <summary>
        /// 连接域,连接成功返回True,失败返回False
        /// </summary>
        /// <param name="domainName"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static bool ConnectAD(string domainName, string userName, string password)
        {
            DirectoryEntry domain = new DirectoryEntry();
            try
            {
                domain.Path = string.Format("LDAP://{0}", domainName);
                domain.Username = userName;
                domain.Password = password;
                domain.AuthenticationType = AuthenticationTypes.Secure;

                domain.RefreshCache();

                domain.Close();
                domain.Dispose();
                return true;

            }
            catch (Exception ex)
            {
                domain.Close();
                domain.Dispose();
            }
            return false;
        }


    }
}
  

  

 

posted @ 2019-03-27 17:04  Jesse!  阅读(1556)  评论(0编辑  收藏  举报