黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

AD用戶操作

系統(tǒng) 2128 0

本人在網(wǎng)上公開(kāi)的源代碼上加上了,搜索,修改,刪除。

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using System.Data;


namespace Common
{
??? public class ADHelper
??? {
??????? private static string DomainName = "VMEX";

??????? private static string LDAPDomain = "DC=VMEX,DC=local";

??????? private static string ADPath = " LDAP://DC=VMEX,DC=local ";

??????? //AD管理員帳號(hào)
??????? private static string ADUser = "Administrator";

??????? //AD管理員密碼
??????? private static string ADPasssWord = " 1234@abcd ";

??????? private static IdentityImpersonation impersonate = new IdentityImpersonation(ADUser, ADPasssWord, DomainName);

??????? ///
??????? ///用戶登錄驗(yàn)證結(jié)果
??????? ///
??????? public enum LoginResult
??????? {
??????????? ///
??????????? ///正常登錄
??????????? ///
??????????? LOGIN_USER_OK = 0,
??????????? ///
??????????? ///用戶不存在
??????????? ///
??????????? LOGIN_USER_DOESNT_EXIST,
??????????? ///
??????????? ///用戶帳號(hào)被禁用
??????????? ///
??????????? LOGIN_USER_ACCOUNT_INACTIVE,
??????????? ///
??????????? ///用戶密碼不正確
??????????? ///
??????????? LOGIN_USER_PASSWORD_INCORRECT
??????? }

??????? ///
??????? ///用戶屬性定義標(biāo)志
??????? ///
??????? public enum ADS_USER_FLAG_ENUM
??????? {
??????????? ///
??????????? ///登錄腳本標(biāo)志。如果通過(guò) ADSI LDAP 進(jìn)行讀或?qū)懖僮鲿r(shí),該標(biāo)志失效。如果通過(guò) ADSI WINNT,該標(biāo)志為只讀。
??????????? ///
??????????? ADS_UF_SCRIPT = 0X0001,
??????????? ///
??????????? ///用戶帳號(hào)禁用標(biāo)志
??????????? ///
??????????? ADS_UF_ACCOUNTDISABLE = 0X0002,
??????????? ///
??????????? ///主文件夾標(biāo)志
??????????? ///
??????????? ADS_UF_HOMEDIR_REQUIRED = 0X0008,
??????????? ///
??????????? ///過(guò)期標(biāo)志
??????????? ///
??????????? ADS_UF_LOCKOUT = 0X0010,
??????????? ///
??????????? ///用戶密碼不是必須的
??????????? ///
??????????? ADS_UF_PASSWD_NOTREQD = 0X0020,
??????????? ///
??????????? ///密碼不能更改標(biāo)志
??????????? ///
??????????? ADS_UF_PASSWD_CANT_CHANGE = 0X0040,
??????????? ///
??????????? ///使用可逆的加密保存密碼
??????????? ///
??????????? ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 0X0080,
??????????? ///
??????????? ///本地帳號(hào)標(biāo)志
??????????? ///
??????????? ADS_UF_TEMP_DUPLICATE_ACCOUNT = 0X0100,
??????????? ///
??????????? ///普通用戶的默認(rèn)帳號(hào)類(lèi)型
??????????? ///
??????????? ADS_UF_NORMAL_ACCOUNT = 0X0200,
??????????? ///
??????????? ///跨域的信任帳號(hào)標(biāo)志
??????????? ///
??????????? ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 0X0800,
??????????? ///
??????????? ///工作站信任帳號(hào)標(biāo)志
??????????? ///
??????????? ADS_UF_WORKSTATION_TRUST_ACCOUNT = 0x1000,
??????????? ///
??????????? ///服務(wù)器信任帳號(hào)標(biāo)志
??????????? ///
??????????? ADS_UF_SERVER_TRUST_ACCOUNT = 0X2000,
??????????? ///
??????????? ///密碼永不過(guò)期標(biāo)志
??????????? ///
??????????? ADS_UF_DONT_EXPIRE_PASSWD = 0X10000,
??????????? ///
??????????? /// MNS 帳號(hào)標(biāo)志
??????????? ///
??????????? ADS_UF_MNS_LOGON_ACCOUNT = 0X20000,
??????????? ///
??????????? ///交互式登錄必須使用智能卡
??????????? ///
??????????? ADS_UF_SMARTCARD_REQUIRED = 0X40000,
??????????? ///
??????????? ///當(dāng)設(shè)置該標(biāo)志時(shí),服務(wù)帳號(hào)(用戶或計(jì)算機(jī)帳號(hào))將通過(guò) Kerberos 委托信任
??????????? ///
??????????? ADS_UF_TRUSTED_FOR_DELEGATION = 0X80000,
??????????? ///
??????????? ///當(dāng)設(shè)置該標(biāo)志時(shí),即使服務(wù)帳號(hào)是通過(guò) Kerberos 委托信任的,敏感帳號(hào)不能被委托
??????????? ///
??????????? ADS_UF_NOT_DELEGATED = 0X100000,
??????????? ///
??????????? ///此帳號(hào)需要 DES 加密類(lèi)型
??????????? ///
??????????? ADS_UF_USE_DES_KEY_ONLY = 0X200000,
??????????? ///
??????????? ///不要進(jìn)行 Kerberos 預(yù)身份驗(yàn)證
??????????? ///
??????????? ADS_UF_DONT_REQUIRE_PREAUTH = 0X4000000,
??????????? ///
??????????? ///用戶密碼過(guò)期標(biāo)志
??????????? ///
??????????? ADS_UF_PASSWORD_EXPIRED = 0X800000,
??????????? ///
??????????? ///用戶帳號(hào)可委托標(biāo)志
??????????? ///
??????????? ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 0X1000000
??????? }

??????? public ADHelper()
??????? {
??????????? //
??????? }

??????? ///
??????? ///獲得DirectoryEntry對(duì)象實(shí)例,以管理員登陸AD
??????? ///
??????? ///
??????? private static DirectoryEntry GetDirectoryObject()
??????? {
??????????? DirectoryEntry entry = new DirectoryEntry(ADPath, ADUser, ADPasssWord, AuthenticationTypes.Secure);
??????????? return entry;
??????? }

??????? ///
??????? ///根據(jù)指定用戶名和密碼獲得相應(yīng)DirectoryEntry實(shí)體
??????? ///
??????? ///
??????? ///
??????? ///
??????? private static DirectoryEntry GetDirectoryObject(string userName, string password)
??????? {
??????????? DirectoryEntry entry = new DirectoryEntry(ADPath, userName, password, AuthenticationTypes.None);
??????????? return entry;
??????? }

??????? private static DirectoryEntry GetDirectoryObject(string domainReference)
??????? {
??????????? DirectoryEntry entry = new DirectoryEntry(ADPath + domainReference, ADUser, ADPasssWord, AuthenticationTypes.Secure);
??????????? return entry;
??????? }

??????? /// <summary>
??????? /// 找到該用戶對(duì)象
??????? /// </summary>
??????? /// <param name="commonName"></param>
??????? /// <returns></returns>
??????? public static DirectoryEntry GetDirectoryEntry(string commonName)
??????? {
??????????? DirectoryEntry de = GetDirectoryObject();
??????????? DirectorySearcher deSearch = new DirectorySearcher(de);
??????????? deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";
??????????? deSearch.SearchScope = SearchScope.Subtree;

??????????? try
??????????? {
??????????????? SearchResult result = deSearch.FindOne();
??????????????? de = new DirectoryEntry(result.Path);
??????????????? return de;
??????????? }
??????????? catch
??????????? {
??????????????? return null;
??????????? }

??????? }

???????? ///
???????? ///根據(jù)用戶公共名稱(chēng)和密碼取得用戶的 對(duì)象。
???????? ///
???????? ///用戶公共名稱(chēng)
???????? ///用戶密碼
???????? ///如果找到該用戶,則返回用戶的 對(duì)象;否則返回 null
???????? public static DirectoryEntry GetDirectoryEntry(string commonName, string password)
???????? {
????????????? DirectoryEntry de = GetDirectoryObject(commonName, password);
????????????? DirectorySearcher deSearch = new DirectorySearcher(de);
????????????? deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";
????????????? deSearch.SearchScope = SearchScope.Subtree;
?
????????????? try
????????????? {
?????????????????? SearchResult result = deSearch.FindOne();
?????????????????? de = new DirectoryEntry(result.Path);
?????????????????? return de;
????????????? }
????????????? catch
????????????? {
?????????????????? return null;
????????????? }
???????? }
?
???????? ///
???????? ///根據(jù)用戶帳號(hào)稱(chēng)取得用戶的 對(duì)象
???????? ///
???????? ///用戶帳號(hào)名
???????? ///如果找到該用戶,則返回用戶的 對(duì)象;否則返回 null
???????? public static DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName)
???????? {
????????????? DirectoryEntry de = GetDirectoryObject();
????????????? DirectorySearcher deSearch = new DirectorySearcher(de);
????????????? deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName=" + sAMAccountName + "))";
????????????? deSearch.SearchScope = SearchScope.Subtree;
?
????????????? try
????????????? {
?????????????????? SearchResult result = deSearch.FindOne();
?????????????????? de = new DirectoryEntry(result.Path);
?????????????????? return de;
????????????? }
????????????? catch
????????????? {
?????????????????? return null;
????????????? }
???????? }
?
???????? ///
???????? ///根據(jù)用戶帳號(hào)和密碼取得用戶的 對(duì)象
???????? ///
???????? ///用戶帳號(hào)名
???????? ///用戶密碼
???????? ///如果找到該用戶,則返回用戶的 對(duì)象;否則返回 null
???????? public static DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName, string password)
???????? {
????????????? DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName);
????????????? if (de != null)
????????????? {
?????????????????? string commonName = de.Properties["cn"][0].ToString();
?
?????????????????? if (GetDirectoryEntry(commonName, password) != null)
?????????????????????? return GetDirectoryEntry(commonName, password);
?????????????????? else
?????????????????????? return null;
????????????? }
????????????? else
????????????? {
?????????????????? return null;
????????????? }
???????? }
?
???????? ///
???????? ///根據(jù)組名取得用戶組的 對(duì)象
???????? ///
???????? ///組名
???????? ///
???????? public static DirectoryEntry GetDirectoryEntryOfGroup(string groupName)
???????? {
????????????? DirectoryEntry de = GetDirectoryObject();
????????????? DirectorySearcher deSearch = new DirectorySearcher(de);
????????????? deSearch.Filter = "(&(objectClass=group)(cn=" + groupName + "))";
????????????? deSearch.SearchScope = SearchScope.Subtree;
?
????????????? try
????????????? {
?????????????????? SearchResult result = deSearch.FindOne();
?????????????????? de = new DirectoryEntry(result.Path);
?????????????????? return de;
????????????? }
????????????? catch
????????????? {
?????????????????? return null;
????????????? }
???????? }
?
?
?
???????? #region GetProperty
?
???????? ///
???????? ///獲得指定 指定屬性名對(duì)應(yīng)的值
???????? ///
???????? ///
???????? ///屬性名稱(chēng)
???????? ///屬性值
???????? public static string GetProperty(DirectoryEntry de, string propertyName)
???????? {
????????????? if(de.Properties.Contains(propertyName))
????????????? {
?????????????????? return de.Properties[propertyName][0].ToString() ;
????????????? }
????????????? else
????????????? {
?????????????????? return string.Empty;
????????????? }
???????? }
?
???????? ///
???????? ///獲得指定搜索結(jié)果 中指定屬性名對(duì)應(yīng)的值
???????? ///
???????? ///
???????? ///屬性名稱(chēng)
???????? ///屬性值
???????? public static string GetProperty(SearchResult searchResult, string propertyName)
???????? {
????????????? if(searchResult.Properties.Contains(propertyName))
????????????? {
?????????????????? return searchResult.Properties[propertyName][0].ToString() ;
????????????? }
????????????? else
????????????? {
?????????????????? return string.Empty;
????????????? }
???????? }
?
???????? #endregion
?
???????? ///
???????? ///設(shè)置指定 的屬性值
???????? ///
???????? ///
???????? ///屬性名稱(chēng)
???????? ///屬性值
???????? public static void SetProperty(DirectoryEntry de, string propertyName, string propertyValue)
???????? {
????????????? if(propertyValue != string.Empty || propertyValue != "" || propertyValue != null)
????????????? {
?????????????????? if(de.Properties.Contains(propertyName))
?????????????????? {
?????????????????????? de.Properties[propertyName][0] = propertyValue;
?????????????????? }
?????????????????? else
?????????????????? {
?????????????????????? de.Properties[propertyName].Add(propertyValue);
?????????????????? }
????????????? }
???????? }
?
???????? ///
???????? ///創(chuàng)建新的用戶
???????? ///
???????? ///N 位置。例如:OU=共享平臺(tái) 或 CN=Users
???????? ///公共名稱(chēng)
???????? ///帳號(hào)
???????? ///密碼
???????? ///physicalDeliveryOfficeName:辦公位置:
???????? ///description:設(shè)備描述:
???????? ///telephoneNumber:固資編碼:
???????? ///department:部門(mén)(二級(jí)部門(mén)):
???????? ///company:公司(一級(jí)部門(mén)):
???????? ///wWWHomePage:IP 地址:
???????? public static DirectoryEntry CreateNewUser(string ldapDN,string snName, string commonName, string sAMAccountName, string password,
???????????? string description,string physicalDeliveryOfficeName,
???????????? string telephoneNumber, string department,string title,
??????????? string company, string wWWHomePage)
???????? {
????????????? DirectoryEntry entry = GetDirectoryObject();
????????????? DirectoryEntry subEntry = entry.Children.Find(ldapDN);
????????????? DirectoryEntry deUser = subEntry.Children.Add("CN=" + commonName, "user");
????????????? deUser.Properties["sAMAccountName"].Value = sAMAccountName;
????????????? //deUser.Properties["description"].Value = description;
????????????? //deUser.Properties["physicalDeliveryOfficeName"].Value = physicalDeliveryOfficeName;
????????????? //deUser.Properties["telephoneNumber"].Value = telephoneNumber;
????????????? //deUser.Properties["department"].Value = department;
????????????? //deUser.Properties["title"].Value = title;
????????????? //deUser.Properties["company"].Value = company;
????????????? //deUser.Properties["wWWHomePage"].Value = wWWHomePage;
????????????? ADHelper.SetProperty(deUser, "sn", snName);
????????????? ADHelper.SetProperty(deUser, "description", description);
????????????? ADHelper.SetProperty(deUser, "physicalDeliveryOfficeName", physicalDeliveryOfficeName);
????????????? ADHelper.SetProperty(deUser, "telephoneNumber", telephoneNumber);
????????????? ADHelper.SetProperty(deUser, "department", department);
????????????? ADHelper.SetProperty(deUser, "title", title);
????????????? ADHelper.SetProperty(deUser, "company", company);
????????????? ADHelper.SetProperty(deUser, "wWWHomePage", wWWHomePage);
????????????? deUser.CommitChanges();
???????????
????????????? //ADHelper.SetProperty(deUser, "description", description);
????????????? ADHelper.SetPassword(commonName, password);
????????????? ADHelper.EnableUser(commonName);
?????????????
????????????? deUser.Close();
????????????? return deUser;
???????? }


??????? /// <summary>
??????? /// 刪除用戶
??????? ///
??????? /// </summary>
??????? /// <param name="ldapDN"></param>
??????? /// <param name="sAMAccountName"></param>
???????? public static void DelUser(string ldapDN, string sAMAccountName)
???????? {
???????????? DirectoryEntry entry = GetDirectoryObject();
???????????? DirectoryEntry subEntry = entry.Children.Find(ldapDN);

???????????? DirectorySearcher deSearch = new DirectorySearcher(subEntry);
??????????? deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName=" + sAMAccountName + "))";
??????????? deSearch.SearchScope = SearchScope.Subtree;
?
??????????? SearchResult result = deSearch.FindOne();
??????????? DirectoryEntry de = new DirectoryEntry(result.Path);
??????????
??????????? subEntry.Children.Remove(de);
?
??????????? entry.CommitChanges();

??????????? de.Close();
???????? }

???????? public static DataTable GetAllUser(string ouName)
???????? {
???????????? DataTable dt = new DataTable();
???????????? dt.Columns.Add("CN");
???????????? dt.Columns.Add("sAMAccountName");
???????????? dt.Columns.Add("description");
???????????? dt.Columns.Add("physicalDeliveryOfficeName");
???????????? dt.Columns.Add("telephoneNumber");
???????????? dt.Columns.Add("department");
???????????? dt.Columns.Add("title");
???????????? dt.Columns.Add("company");
???????????? dt.Columns.Add("wWWHomePage");

???????????? DirectoryEntry adRoot = GetDirectoryObject();
???????????? //設(shè)備MAC認(rèn)證
???????????? DirectoryEntry ou = adRoot.Children.Find("OU=" + ouName);

???????????? DirectorySearcher mySearcher = new DirectorySearcher(ou);
???????????? //(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))
???????????? //mySearcher.Filter = ("(objectClass=user)");
???????????? mySearcher.Filter = "(&(&(objectCategory=person)(objectClass=user)))";

???????????? foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
???????????? {
???????????????? DataRow dr = dt.NewRow();
???????????????? dr["CN"] = string.Empty;
???????????????? dr["sAMAccountName"] = string.Empty;
???????????????? dr["description"] = string.Empty;
???????????????? dr["physicalDeliveryOfficeName"] = string.Empty;
???????????????? dr["telephoneNumber"] = string.Empty;
???????????????? dr["department"] = string.Empty;
???????????????? dr["title"] = string.Empty;
???????????????? dr["company"] = string.Empty;
???????????????? dr["wWWHomePage"] = string.Empty;


???????????????? DirectoryEntry user = resEnt.GetDirectoryEntry();

???????????????? if (user.Properties.Contains("sAMAccountName"))
???????????????? {
???????????????????? dr["sAMAccountName"] = user.Properties["sAMAccountName"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("CN"))
???????????????? {
???????????????????? dr["CN"] = user.Properties["CN"][0].ToString();
???????????????? }

???????????????? if (user.Properties.Contains("description"))
???????????????? {
???????????????????? dr["description"] = user.Properties["description"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("physicalDeliveryOfficeName"))
???????????????? {
???????????????????? dr["physicalDeliveryOfficeName"] = user.Properties["physicalDeliveryOfficeName"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("telephoneNumber"))
???????????????? {
???????????????????? dr["telephoneNumber"] = user.Properties["telephoneNumber"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("department"))
???????????????? {
???????????????????? dr["department"] = user.Properties["department"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("title"))
???????????????? {
???????????????????? dr["title"] = user.Properties["title"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("company"))
???????????????? {
???????????????????? dr["company"] = user.Properties["company"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("wWWHomePage"))
???????????????? {
???????????????????? dr["wWWHomePage"] = user.Properties["wWWHomePage"][0].ToString();
???????????????? }
???????????????? dt.Rows.Add(dr);

???????????? }

???????????? return dt;
???????? }

?????? ? /// <summary>
??????? /// 查詢用戶
??????? /// </summary>
??????? /// <param name="ouName"></param>
??????? /// <returns></returns>
???????? public static DataTable GetAdUser(string ouName, string commonName)
???????? {
???????????? DataTable dt = new DataTable();
???????????? dt.Columns.Add("cn");
???????????? dt.Columns.Add("sAMAccountName");
???????????? dt.Columns.Add("description");
???????????? dt.Columns.Add("physicalDeliveryOfficeName");
???????????? dt.Columns.Add("telephoneNumber");
???????????? dt.Columns.Add("department");
???????????? dt.Columns.Add("title");
???????????? dt.Columns.Add("company");
???????????? dt.Columns.Add("wWWHomePage");

???????????? DirectoryEntry adRoot = GetDirectoryObject();
???????????? //設(shè)備MAC認(rèn)證
???????????? DirectoryEntry ou = adRoot.Children.Find("OU=" + ouName);

???????????? DirectorySearcher mySearcher = new DirectorySearcher(ou);
???????????? //(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))
???????????? //mySearcher.Filter = ("(objectClass=user)");
???????????? mySearcher.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";

???????????? foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
???????????? {
???????????????? DataRow dr = dt.NewRow();
???????????????? dr["cn"] = string.Empty;
???????????????? dr["sAMAccountName"] = string.Empty;
???????????????? dr["description"] = string.Empty;
???????????????? dr["physicalDeliveryOfficeName"] = string.Empty;
???????????????? dr["telephoneNumber"] = string.Empty;
???????????????? dr["department"] = string.Empty;
???????????????? dr["title"] = string.Empty;
???????????????? dr["company"] = string.Empty;
???????????????? dr["wWWHomePage"] = string.Empty;
????????????????

???????????????? DirectoryEntry user = resEnt.GetDirectoryEntry();
???????????????? if (user.Properties.Contains("cn"))
???????????????? {
???????????????????? dr["cn"] = user.Properties["cn"][0].ToString();
???????????????? }

???????????????? if (user.Properties.Contains("sAMAccountName"))
???????????????? {
???????????????????? dr["sAMAccountName"] = user.Properties["sAMAccountName"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("description"))
???????????????? {
???????????????????? dr["description"] = user.Properties["description"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("physicalDeliveryOfficeName"))
???????????????? {
???????????????????? dr["physicalDeliveryOfficeName"] = user.Properties["physicalDeliveryOfficeName"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("telephoneNumber"))
???????????????? {
???????????????????? dr["telephoneNumber"] = user.Properties["telephoneNumber"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("department"))
???????????????? {
???????????????????? dr["department"] = user.Properties["department"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("title"))
???????????????? {
???????????????????? dr["title"] = user.Properties["title"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("company"))
???????????????? {
???????????????????? dr["company"] = user.Properties["company"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("wWWHomePage"))
???????????????? {
???????????????????? dr["wWWHomePage"] = user.Properties["wWWHomePage"][0].ToString();
???????????????? }
???????????????? dt.Rows.Add(dr);

???????????? }

???????????? return dt;

???????? }


??????? /// <summary>
??????? ///
??????? /// </summary>
??????? /// <param name="ouName"></param>
??????? /// <param name="commonName">登陸帳號(hào)</param>
??????? /// <returns></returns>
???????? public static DataTable GetUser(string ouName, string commonName)
???????? {
???????????? DataTable dt = new DataTable();
???????????? dt.Columns.Add("cn");
???????????? dt.Columns.Add("sAMAccountName");
???????????? dt.Columns.Add("description");
???????????? dt.Columns.Add("physicalDeliveryOfficeName");
???????????? dt.Columns.Add("telephoneNumber");
???????????? dt.Columns.Add("department");
???????????? dt.Columns.Add("title");
???????????? dt.Columns.Add("company");
???????????? dt.Columns.Add("wWWHomePage");

???????????? DirectoryEntry adRoot = GetDirectoryObject();
???????????? //設(shè)備MAC認(rèn)證
???????????? DirectoryEntry ou = adRoot.Children.Find("OU=" + ouName);

???????????? DirectorySearcher mySearcher = new DirectorySearcher(ou);
???????????? //(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))
???????????? //mySearcher.Filter = ("(objectClass=user)");
???????????? mySearcher.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName=" + commonName + "))";

???????????? foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
???????????? {
???????????????? DataRow dr = dt.NewRow();
???????????????? dr["cn"] = string.Empty;
???????????????? dr["sAMAccountName"] = string.Empty;
???????????????? dr["description"] = string.Empty;
???????????????? dr["physicalDeliveryOfficeName"] = string.Empty;
???????????????? dr["telephoneNumber"] = string.Empty;
???????????????? dr["department"] = string.Empty;
???????????????? dr["title"] = string.Empty;
???????????????? dr["company"] = string.Empty;
???????????????? dr["wWWHomePage"] = string.Empty;


???????????????? DirectoryEntry user = resEnt.GetDirectoryEntry();
???????????????? if (user.Properties.Contains("cn"))
???????????????? {
???????????????????? dr["cn"] = user.Properties["cn"][0].ToString();
???????????????? }

???????????????? if (user.Properties.Contains("sAMAccountName"))
???????????????? {
???????????????????? dr["sAMAccountName"] = user.Properties["sAMAccountName"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("description"))
???????????????? {
???????????????????? dr["description"] = user.Properties["description"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("physicalDeliveryOfficeName"))
???????????????? {
???????????????????? dr["physicalDeliveryOfficeName"] = user.Properties["physicalDeliveryOfficeName"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("telephoneNumber"))
???????????????? {
???????????????????? dr["telephoneNumber"] = user.Properties["telephoneNumber"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("department"))
???????????????? {
???????????????????? dr["department"] = user.Properties["department"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("title"))
???????????????? {
???????????????????? dr["title"] = user.Properties["title"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("company"))
???????????????? {
???????????????????? dr["company"] = user.Properties["company"][0].ToString();
???????????????? }
???????????????? if (user.Properties.Contains("wWWHomePage"))
???????????????? {
???????????????????? dr["wWWHomePage"] = user.Properties["wWWHomePage"][0].ToString();
???????????????? }
???????????????? dt.Rows.Add(dr);

???????????? }

???????????? return dt;

???????? }
?
???????? ///
???????? ///創(chuàng)建新的用戶。默認(rèn)創(chuàng)建在 Users 單元下。
???????? ///
???????? ///公共名稱(chēng)
???????? ///帳號(hào)
???????? ///密碼
???????? ///
???????? public static DirectoryEntry CreateNewUser(string commonName,string snName, string sAMAccountName,
?????????????? string password,
?????????????? string description,string physicalDeliveryOfficeName,
???????????? string telephoneNumber, string department,string title,
??????????? string company, string wWWHomePage)
???????? {
???????????? return CreateNewUser("OU=設(shè)備MAC認(rèn)證", commonName,snName, sAMAccountName, password,
?????????????????? description,physicalDeliveryOfficeName,
???????????? telephoneNumber, department,title,
???????????? company, wWWHomePage);
???????? }
?
???????? ///
???????? ///判斷指定公共名稱(chēng)的用戶是否存在
???????? ///
???????? ///用戶公共名稱(chēng)
???????? ///如果存在,返回 true;否則返回 false
???????? public static bool IsUserExists(string commonName)
???????? {
????????????? DirectoryEntry de = GetDirectoryObject();
????????????? DirectorySearcher deSearch = new DirectorySearcher(de);
????????????? deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";?????? // LDAP 查詢串
????????????? SearchResultCollection results = deSearch.FindAll();
?
????????????? if (results.Count == 0)
?????????????????? return false;
????????????? else
?????????????????? return true;
???????? }
?
???????? ///
???????? ///判斷用戶帳號(hào)是否激活
???????? ///
???????? ///用戶帳號(hào)屬性控制器
???????? ///如果用戶帳號(hào)已經(jīng)激活,返回 true;否則返回 false
???????? public static bool IsAccountActive(int userAccountControl)
???????? {
????????????? int userAccountControl_Disabled = Convert.ToInt32(ADS_USER_FLAG_ENUM.ADS_UF_ACCOUNTDISABLE);
????????????? int flagExists = userAccountControl & userAccountControl_Disabled;
?
????????????? if (flagExists > 0)
?????????????????? return false;
????????????? else
?????????????????? return true;
???????? }
?
???????? ///
???????? ///判斷用戶與密碼是否足夠以滿足身份驗(yàn)證進(jìn)而登錄
???????? ///
???????? ///用戶公共名稱(chēng)
???????? ///密碼
???????? ///如能可正常登錄,則返回 true;否則返回 false
???????? public static LoginResult Login(string commonName, string password)
???????? {
????????????? DirectoryEntry de = GetDirectoryEntry(commonName);
?
????????????? if (de != null)
????????????? {
?????????????????? // 必須在判斷用戶密碼正確前,對(duì)帳號(hào)激活屬性進(jìn)行判斷;否則將出現(xiàn)異常。
?????????????????? int userAccountControl = Convert.ToInt32(de.Properties["userAccountControl"][0]);
?????????????????? de.Close();
?
?????????????????? if (!IsAccountActive(userAccountControl))
?????????????????????? return LoginResult.LOGIN_USER_ACCOUNT_INACTIVE;
?
?????????????????? if (GetDirectoryEntry(commonName, password) != null)
?????????????????????? return LoginResult.LOGIN_USER_OK;
?????????????????? else
?????????????????????? return LoginResult.LOGIN_USER_PASSWORD_INCORRECT;
????????????? }
????????????? else
????????????? {
?????????????????? return LoginResult.LOGIN_USER_DOESNT_EXIST;
????????????? }
???????? }
?
???????? ///
???????? ///判斷用戶帳號(hào)與密碼是否足夠以滿足身份驗(yàn)證進(jìn)而登錄
???????? ///
???????? ///用戶帳號(hào)
???????? ///密碼
???????? ///如能可正常登錄,則返回 true;否則返回 false
???????? public static LoginResult LoginByAccount(string sAMAccountName, string password)
???????? {
????????????? DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName);
??????????????????
????????????? if (de != null)
????????????? {
?????????????????? // 必須在判斷用戶密碼正確前,對(duì)帳號(hào)激活屬性進(jìn)行判斷;否則將出現(xiàn)異常。
?????????????????? int userAccountControl = Convert.ToInt32(de.Properties["userAccountControl"][0]);
?????????????????? de.Close();
?
?????????????????? if (!IsAccountActive(userAccountControl))
?????????????????????? return LoginResult.LOGIN_USER_ACCOUNT_INACTIVE;
?
?????????????????? if (GetDirectoryEntryByAccount(sAMAccountName, password) != null)
?????????????????????? return LoginResult.LOGIN_USER_OK;
?????????????????? else
?????????????????????? return LoginResult.LOGIN_USER_PASSWORD_INCORRECT;
????????????? }
????????????? else
????????????? {
?????????????????? return LoginResult.LOGIN_USER_DOESNT_EXIST;
????????????? }
???????? }
?
???????? ///
???????? ///設(shè)置用戶密碼,管理員可以通過(guò)它來(lái)修改指定用戶的密碼。
???????? ///
???????? ///用戶公共名稱(chēng)
???????? ///用戶新密碼
???????? public static void SetPassword(string commonName, string newPassword)
???????? {
????????????? DirectoryEntry de = GetDirectoryEntry(commonName);
?????????????
????????????? // 模擬超級(jí)管理員,以達(dá)到有權(quán)限修改用戶密碼
????????????? impersonate.BeginImpersonate();
????????????? de.Invoke("SetPassword", new object[]{newPassword});
????????????? impersonate.StopImpersonate();
?
????????????? de.Close();
???????? }

???????? ///
???????? ///啟用指定公共名稱(chēng)的用戶
???????? ///
???????? ///用戶公共名稱(chēng)
???????? public static void EnableUser(string commonName)
???????? {
???????????? EnableUser(GetDirectoryEntry(commonName));
???????? }
???????? ///
???????? ///啟用指定 的用戶
???????? ///
???????? ///
???????? public static void EnableUser(DirectoryEntry de)
???????? {
???????????? impersonate.BeginImpersonate();
???????????? de.Properties["userAccountControl"][0] = ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_NORMAL_ACCOUNT | ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_DONT_EXPIRE_PASSWD;
???????????? de.CommitChanges();
???????????? impersonate.StopImpersonate();
???????????? de.Close();
???????? }
?
???????? ///
???????? ///設(shè)置帳號(hào)密碼,管理員可以通過(guò)它來(lái)修改指定帳號(hào)的密碼。
???????? ///
???????? ///用戶帳號(hào)
???????? ///用戶新密碼
???????? public static void SetPasswordByAccount(string sAMAccountName, string newPassword)
???????? {
????????????? DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName);
?
????????????? // 模擬超級(jí)管理員,以達(dá)到有權(quán)限修改用戶密碼
????????????? IdentityImpersonation impersonate = new IdentityImpersonation(ADUser, ADPasssWord, LDAPDomain);
????????????? impersonate.BeginImpersonate();
????????????? de.Invoke("SetPassword", new object[]{newPassword});
????????????? impersonate.StopImpersonate();
?
????????????? de.Close();
???????? }

????? ?? /// <summary>
??????? /// 修改用戶資料
??????? /// </summary>
??????? /// <param name="sAMAccountName"></param>
???????? public static void EditUser(string ldapDN, string sAMAccountName,
???????????? string snName, string description, string physicalDeliveryOfficeName,
???????????? string telephoneNumber, string department, string title
???????????? , string wWWHomePage, string company
???????????? )
???????? {
???????????? DirectoryEntry entry = GetDirectoryObject();
???????????? DirectoryEntry subEntry = entry.Children.Find(ldapDN);

???????????? DirectorySearcher deSearch = new DirectorySearcher(subEntry);
???????????? deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName=" + sAMAccountName + "))";
???????????? deSearch.SearchScope = SearchScope.Subtree;

???????????? SearchResult result = deSearch.FindOne();
???????????? DirectoryEntry de = new DirectoryEntry(result.Path);

???????????? ADHelper.SetProperty(de, "sn", snName);
???????????? ADHelper.SetProperty(de, "description", description);
???????????? ADHelper.SetProperty(de, "physicalDeliveryOfficeName", physicalDeliveryOfficeName);
???????????? ADHelper.SetProperty(de, "telephoneNumber", telephoneNumber);
???????????? ADHelper.SetProperty(de, "department", department);
???????????? ADHelper.SetProperty(de, "title", title);
???????????? ADHelper.SetProperty(de, "company", company);
???????????? ADHelper.SetProperty(de, "wWWHomePage", wWWHomePage);

???????????? de.CommitChanges();

???????????? de.Close();????
??????????????? }


?
???????? ///
???????? ///修改用戶密碼
???????? ///
???????? ///用戶公共名稱(chēng)
???????? ///舊密碼
???????? ///新密碼
???????? public static void ChangeUserPassword (string commonName, string oldPassword, string newPassword)
???????? {
????????????? // to-do: 需要解決密碼策略問(wèn)題
????????????? DirectoryEntry oUser = GetDirectoryEntry(commonName);
????????????? oUser.Invoke("ChangePassword", new Object[]{oldPassword, newPassword});
????????????? oUser.Close();
???????? }
?
??????
?
????????
???????? ///
???????? ///禁用指定公共名稱(chēng)的用戶
???????? ///
???????? ///用戶公共名稱(chēng)
???????? public static void DisableUser(string commonName)
???????? {
????????????? DisableUser(GetDirectoryEntry(commonName));
???????? }
?
???????? ///
???????? ///禁用指定 的用戶
???????? ///
???????? ///
???????? public static void DisableUser(DirectoryEntry de)
???????? {
????????????? impersonate.BeginImpersonate();
????????????? de.Properties["userAccountControl"][0]=ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_NORMAL_ACCOUNT | ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_DONT_EXPIRE_PASSWD | ADHelper.ADS_USER_FLAG_ENUM.ADS_UF_ACCOUNTDISABLE;
????????????? de.CommitChanges();
????????????? impersonate.StopImpersonate();
????????????? de.Close();
???????? }
?
???????? ///
???????? ///將指定的用戶添加到指定的組中。默認(rèn)為 Users 下的組和用戶。
???????? ///
???????? ///用戶公共名稱(chēng)
???????? ///組名
???????? public static void AddUserToGroup(string userCommonName, string groupName)
????????? {
????????????? DirectoryEntry oGroup = GetDirectoryEntryOfGroup(groupName);
????????????? DirectoryEntry oUser = GetDirectoryEntry(userCommonName);
?????????????
????????????? impersonate.BeginImpersonate();
????????????? oGroup.Properties["member"].Add(oUser.Properties["distinguishedName"].Value);
????????????? oGroup.CommitChanges();
????????????? impersonate.StopImpersonate();
?
????????????? oGroup.Close();
????????????? oUser.Close();
???????? }
?
???????? ///
???????? ///將用戶從指定組中移除。默認(rèn)為 Users 下的組和用戶。
???????? ///
???????? ///用戶公共名稱(chēng)
???????? ///組名
???????? public static void RemoveUserFromGroup(string userCommonName, string groupName)
???????? {
????????????? DirectoryEntry oGroup = GetDirectoryEntryOfGroup(groupName);
????????????? DirectoryEntry oUser = GetDirectoryEntry(userCommonName);
?????????????
????????????? impersonate.BeginImpersonate();
????????????? oGroup.Properties["member"].Remove(oUser.Properties["distinguishedName"].Value);
????????????? oGroup.CommitChanges();
????????????? impersonate.StopImpersonate();
?
????????????? oGroup.Close();
????????????? oUser.Close();
???????? }
?
???? }

???
}

AD用戶操作


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論