C#域控操作大全节点删除修改密码添加用户.docx
- 文档编号:25604469
- 上传时间:2023-06-10
- 格式:DOCX
- 页数:58
- 大小:24.42KB
C#域控操作大全节点删除修改密码添加用户.docx
《C#域控操作大全节点删除修改密码添加用户.docx》由会员分享,可在线阅读,更多相关《C#域控操作大全节点删除修改密码添加用户.docx(58页珍藏版)》请在冰豆网上搜索。
C#域控操作大全节点删除修改密码添加用户
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Collections;
usingSystem.Data;
usingSystem.DirectoryServices;
usingNJZF.SCDM.DAL.Database;
usingSystem.Data.SqlClient;
usingNJZF.SCDM.DAL.DBUtility;
usingNJZF.SCDM.BLL.Tools;
usingNJZF.SCDM.DataModel;
namespaceNJZF.SCDM.BLL.User
{
publicclassAdHelper
{
///
///定义域控配置结构
///
publicstructDomainConfig
{
publicstringLdapPath;
publicstringAdUser;
publicstringAdPwd;
publicstringDomainName;
}
///
///初始化域控配置结构
///
///
publicstaticDomainConfigGetConfig()
{
DomainConfigConfig=newDomainConfig();
SqlParameter[]Parameter={newSqlParameter
("@Type",SqlDbType.Int,4)};
Parameter[0].Value=1;
DataSetds=DbHelperSQL.RunProcedure("SCDM_Config_GetModel",
Parameter,"SCDM_Config");
if(ds.Tables[0].Rows.Count>0)
{
Config.LdapPath=ds.Tables[0].Rows[0][1].ToString();
Config.AdUser=ds.Tables[0].Rows[0][2].ToString();
Config.AdPwd=ds.Tables[0].Rows[0][3].ToString();
stringDomainName="";
string[]DomainNameArr=StringTools.GetSafeStr(ds.Tables
[0].Rows[0][5].ToString()).Split(newchar[]{'.'});
if(DomainNameArr.Length>0)
{
foreach(stringarrnameinDomainNameArr)
{
stringDC1="DC="+arrname+",";
DomainName+=DC1;
}
}
DomainName=DomainName.Remove(DomainName.LastIndexOf(','));
Config.DomainName=DomainName;
}
returnConfig;
}
///
///获取数据库中跟节点的名称
///
///
publicstaticstringGetRootName()
{
SqlParameter[]Parameter={newSqlParameter("@Type",
SqlDbType.Int,4)};
Parameter[0].Value=1;
DataSetds=DbHelperSQL.RunProcedure("SCDM_Config_GetModel",
Parameter,"SCDM_Config");
if(ds.Tables[0].Rows.Count>0)
{
returnds.Tables[0].Rows[0][1].ToString();
}
else
{
return"";
}
}
///
///域的名称
///
#regionMyRegion
///用户属性定义标志
publicenumADS_USER_FLAG_ENUM
{
///
///登录脚本标志。
如果通过ADSILDAP进行读或写操作时,该标志失效
。
如果通过ADSIWINNT,该标志为只读。
///
ADS_UF_SCRIPT=0X0001,
///
///用户帐号禁用标志
///
ADS_UF_ACCOUNTDISABLE=0X0002,
///
///主文件夹标志
///
ADS_UF_HOMEDIR_REQUIRED=0X0008,
///
///过期标志
///
ADS_UF_LOCKOUT=0X0010,
///
///用户密码不是必须的
///
ADS_UF_PASSWD_NOTREQD=0X0020,
///
///密码不能更改标志
///
ADS_UF_PASSWD_CANT_CHANGE=0X0040,
///
///使用可逆的加密保存密码
///
ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED=0X0080,
///
///本地帐号标志
///
ADS_UF_TEMP_DUPLICATE_ACCOUNT=0X0100,
///
///普通用户的默认帐号类型
///
ADS_UF_NORMAL_ACCOUNT=0X0200,
///
///跨域的信任帐号标志
///
ADS_UF_INTERDOMAIN_TRUST_ACCOUNT=0X0800,
///
///工作站信任帐号标志
///
ADS_UF_WORKSTATION_TRUST_ACCOUNT=0x1000,
///
///服务器信任帐号标志
///
ADS_UF_SERVER_TRUST_ACCOUNT=0X2000,
///
///密码永不过期标志
///
ADS_UF_DONT_EXPIRE_PASSWD=0X10000,
///
///MNS帐号标志
///
ADS_UF_MNS_LOGON_ACCOUNT=0X20000,
///
///交互式登录必须使用智能卡
///
ADS_UF_SMARTCARD_REQUIRED=0X40000,
///
///当设置该标志时,服务帐号(用户或计算机帐号)将通过Kerberos委
托信任
///
ADS_UF_TRUSTED_FOR_DELEGATION=0X80000,
///
///当设置该标志时,即使服务帐号是通过Kerberos委托信任的,敏感帐
号不能被委托
///
ADS_UF_NOT_DELEGATED=0X100000,
///
///此帐号需要DES加密类型
///
ADS_UF_USE_DES_KEY_ONLY=0X200000,
///
///不要进行Kerberos预身份验证
///
ADS_UF_DONT_REQUIRE_PREAUTH=0X4000000,
///用户密码过期标志
ADS_UF_PASSWORD_EXPIRED=0X800000,
///用户帐号可委托标志
ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION=0X1000000
}
#endregion
#region获取域上的所有的用户列表
///获取域上的所有的用户列表
///
///
publicstaticArrayListGetUsers()
{
ArrayListusers=newArrayList();
DirectoryEntryde=GetObjectByAdmin();
DirectorySearcherdeSearch=newDirectorySearcher(de);
deSearch.Filter="(&(&(objectCategory=person)
(objectClass=user)))";
deSearch.SearchScope=SearchScope.Subtree;
try
{
SearchResultCollectionresults=deSearch.FindAll();
for(inti=0;i { DirectoryEntrydes=results[i].GetDirectoryEntry(); users.Add(des); } returnusers; } catch(Exceptione) { returnnull; } } #endregion #region获取域上的所有用户组列表 ///获取域上的所有用户组列表 /// /// publicstaticArrayListGetGroups() { ArrayListgroups=newArrayList(); DirectoryEntryde=GetObjectByAdmin(); DirectorySearcherdeSearch=newDirectorySearcher(de); deSearch.Filter="(objectCategory=group)"; deSearch.SearchScope=SearchScope.Subtree; SearchResultCollectionresults; try { results=deSearch.FindAll(); foreach(SearchResultsrinresults) { DirectoryEntrydirectoryentry=sr.GetDirectoryEntry(); groups.Add(directoryentry); } returngroups; } catch(Exceptione) { returnnull; } } #endregion #region根据用户组的名称查找该组下的用户成员 ///根据用户组的名称查找该组下的用户成员 /// /// /// publicstaticArrayListGetUsersByGroup(stringgroupName) { ArrayListusers=newArrayList(); DirectoryEntrySearchRoot=GetObjectByAdmin(); DirectorySearcherdirectorySearch=newDirectorySearcher (SearchRoot); directorySearch.Filter="(&(objectClass=group)(SAMAccountName=" +groupName+"))"; SearchResultresults=directorySearch.FindOne(); try { if(results! =null) { DirectoryEntrydirEntry=newDirectoryEntry (results.Path,GetConfig().AdUser,GetConfig().AdPwd); System.DirectoryServices.PropertyCollection propertyCollection=dirEntry.Properties; intcount=propertyCollection["member"].Count; for(inti=0;i { stringrespath=results.Path; string[]pathnavigate=respath.Split ("CN".ToCharArray()); respath=pathnavigate[0]; stringobjpath=propertyCollection["member"] [i].ToString(); stringpath=respath+objpath; DirectoryEntryuser=newDirectoryEntry(path, GetConfig().AdUser,GetConfig().AdPwd); users.Add(user); } } returnusers; } catch(Exceptione) { returnnull; } } #endregion #region根据用户组查找对应的用户 ///根据用户组查找对应的用户 /// /// publicstaticArrayListGetUsersByUserGroup(stringGroupName) { ArrayListusers=newArrayList(); DirectoryEntrySearchRoot=GetObjectByAdmin(); DirectorySearcherdirectorySearch=newDirectorySearcher (SearchRoot); directorySearch.Filter="(&(objectClass=group)(SAMAccountName=" +GroupName+"))"; SearchResultresults=directorySearch.FindOne(); try { if(results! =null) { DirectoryEntrydirEntry=newDirectoryEntry (results.Path,GetConfig().AdUser,GetConfig().AdPwd); System.DirectoryServices.PropertyCollection propertyCollection=dirEntry.Properties; intcount=propertyCollection["member"].Count; for(inti=0;i { stringrespath=results.Path; string[]pathnavigate=respath.Split ("CN".ToCharArray()); respath=pathnavigate[0]; stringobjpath=propertyCollection["member"] [i].ToString(); stringpath=respath+objpath; DirectoryEntryuser=newDirectoryEntry(path, GetConfig().AdUser,GetConfig().AdPwd); users.Add(user); } } else { users=null; } returnusers; } catch(Exceptione) { returnnull; } } #endregion #region根据用户名称查找对应的组 ///根据用户名称查找对应的组 /// /// /// publicstaticArrayListGetGroupsByUser(stringusername) { DirectoryEntrydirectoryentry=GetObjectByAdmin(); DirectorySearcherds=newDirectorySearcher(directoryentry); ds.Filter="(&(sAMAccountName="+username+"))"; ds.PropertiesToLoad.Add("memberof"); SearchResultsearchresult=ds.FindOne(); ArrayListresults=newArrayList(); if(searchresult==null) { results=null; } else { if(searchresult.Properties["memberof"]==null) { return(null); } for(inti=0;i ["memberof"].Count;i++) { stringtheGroupPath=searchresult.Properties ["memberof"][i].ToString(); results.Add(theGroupPath.Substring(3, theGroupPath.IndexOf(",")-3)); } } directoryentry.Close(); return(results); } #endregion #region以管理员的身份登陆域 ///以管理员的身份登陆域 /// /// /// /// /// publicstaticDirectoryEntryGetObjectByAdmin() { DirectoryEntryentry=newDirectoryEntry(GetConfig ().LdapPath,GetConfig().AdUser,GetConfig().AdPwd, AuthenticationTypes.Secure); returnentry; } /// ///测试域控链接的方法 /// /// /// publicstaticDirectoryEntryTestAD(stringUserName) { DirectoryEntryde=GetObjectByAdmin(); DirectorySearcherdeSearch=newDirectorySearcher(de); deSearch.Filter="(&(objectClass=group)(cn="+UserName+"))"; deSearch.SearchScope=SearchScope.Subtree; try { SearchResultresult=deSearch.FindOne(); de=newDirectoryEntry(result.Path); returnde; } catch { returnnull; } } #endregion #region以管理员的身份登陆域 /// ///以管理员的身份登陆域 /// /// /// /// /// privatestaticDirectoryEntryGetDirectoryObject(string domainReference) { DirectoryEntryentry=newDirectoryEntry(GetConfig().LdapPath+ domainReference,GetConfig().AdUser,GetConfig().AdPwd, AuthenticationTypes.Secure); returnentry; } #endregion #region根据用户名称查找对应的组 //
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 操作 大全 节点 删除 修改 密码 添加 用户
![提示](https://static.bdocx.com/images/bang_tan.gif)