ActiveDirectoryLib
1
// CreateBy : xuzhong2
// CreateTime : 2003-08-083
// Version : 1.04
using System;5

6

namespace XZSoftware.ActiveDirectoryLib
{7

/**//// <summary>8
/// IADObject活动目录基本接口9
/// </summary>10

public interface IADObject
{11

/**//// <summary>12
/// 获得显示名13
/// </summary>14

string Name
{15
get;16
}17

18

/**//// <summary>19
/// 获得adsPath路径20
/// </summary>21

string AdsPath
{22
get;23
}24

25

/**//// <summary>26
/// 获取 DirectoryEntry 的全局唯一标识符 (GUID)。27
/// </summary>28

Guid Guid
{29
get;30
}31
}32
}33

1

/**//*2
* CreateBy : xuzhong3
* CreateTime : 2003-08-084
* Version : 0.15
* 6
*/7

8
using System;9
using System.DirectoryServices ;10

11

namespace XZSoftware.ActiveDirectoryLib
{12

/**//// <summary>13
/// ADObject 的摘要说明。14
/// </summary>15

public class ADObject:IADObject
{16
17

/**//// <summary>18
/// 对AD进行处理所要用的工具19
/// </summary>20
public static IADUtility Utility;21

22

/**//// <summary>23
/// 当前DirectoryEntry实例对象24
/// </summary>25
private DirectoryEntry m_directoryEntry;26
27

public ADObject()
{28
}29

/**//// <summary>30
/// 通过DirectoryEntry对象实例化该类31
/// </summary>32
/// <param name="directoryEntry">directoryEntry对象</param>33

public ADObject(DirectoryEntry directoryEntry)
{34
this.m_directoryEntry = directoryEntry ;35
}36

37

IADObject 成员#region IADObject 成员 38

public string AdsPath
{39

get
{40
return this.m_directoryEntry.Path;41
}42
}43
44

/**//// <summary>45
/// 返回Name46
/// </summary>47

public string Name
{48

get
{49
return this.m_directoryEntry.Name;50
}51
}52
53

/**//// <summary>54
/// 返回唯一Guid55
/// </summary>56

public Guid Guid
{57

get
{58
return this.m_directoryEntry.Guid;59
}60
}61

62
#endregion63

64

65

/**//// <summary>66
/// 设置指定属性的值67
/// </summary>68
/// <param name="property">属性名</param>69
/// <param name="value">更新数据</param>70

public void SetProperty(string property, object value)
{71
int propertyCount ;72

try
{73
propertyCount = this.m_directoryEntry.Properties[property].Count ;74

}catch
{75
throw( new ADPropertyException("当前对象没有'" 76
+ property + "'属性,操作不被允许。"));77
}78

if (propertyCount !=0)
{79
this.m_directoryEntry.Properties[property][0] = value ;80

} else
{81
this.m_directoryEntry.Properties[property].Add(value) ;82
}83

try
{84
this.m_directoryEntry.CommitChanges() ;85

}catch
{86
throw( new ADPropertyException("更新属性'"87
+property+"'失败,操作不被允许,"88
+ "可能是当前用户没有权限。"));89
}90
}91

92

/**//// <summary>93
/// 获取指定属性名的第一个值94
/// </summary>95
/// <param name="property"></param>96
/// <returns></returns>97

public object GetProperty(string property)
{98
int propertyCount ;99

try
{100
propertyCount = this.m_directoryEntry.Properties[property].Count ;101

}catch
{102
throw( new ADPropertyException("当前对象没有'" 103
+ property + "'属性,无法取得该值。"));104
}105

if(propertyCount != 0)
{106

try
{107
return this.m_directoryEntry.Properties[property][0] ;108

}catch(Exception exc)
{109
throw new ADException("读取"+property+"属性时出错。",exc);110
}111

}else
{112
return null ;113
}114
}115

116

public System.Collections.IList GetProperties(string property)
{117
System.Collections.IList ps = null ;118
int propertyCount ;119

try
{120
propertyCount = this.m_directoryEntry.Properties[property].Count ;121

}catch
{122
throw( new ADPropertyException("当前对象没有'" 123
+ property + "'属性,无法取得该值。"));124
}125

if (propertyCount >= 0 )
{126
ps = new System.Collections.ArrayList();127

for(int index = 0 ; index < propertyCount ; index++)
{128
ps.Add(this.m_directoryEntry.Properties[property][index]);129
}130
}131
return ps;132
}133

134

/**//// <summary>135
/// 获得当前对象的DirectoryEntry表示形式136
/// </summary>137

public DirectoryEntry MyDirectoryEntry
{138

get
{139
return this.m_directoryEntry ;140
}141

set
{142

if ( value == null )
{143
throw ( new ADObjectEmptyException ("初始化ADObject出错,"144
+"请确认初始化使用的数据是否有效。"));145
}146
this.m_directoryEntry = value ;147
}148
}149
}150

151

public class ADPropertyException : Exception
{152
private string m_strMessage ;153

public ADPropertyException():base()
{154
m_strMessage = "修改属性时出错";155
}156

157

/**//// <summary>158
/// 实例化159
/// </summary>160
/// <param name="message">错误信息</param>161

public ADPropertyException(string message ):base(message)
{162
this.m_strMessage = message ;163
}164
165

/**//// <summary>166
/// 指定出错显示的信息167
/// </summary>168

public override string Message
{169

get
{170
return this.m_strMessage;171
}172
}173
174

/**//// <summary>175
/// 返回它的字符表示176
/// </summary>177
/// <returns></returns>178

public override string ToString()
{179
return base.ToString ();180
}181
}182

183

public class ADObjectEmptyException : Exception
{184
private string m_strMessage ;185

public ADObjectEmptyException():base()
{186
m_strMessage = "实例化出错。";187
}188

public ADObjectEmptyException(string message ):base(message)
{189
this.m_strMessage = message ;190
}191
192

public override string Message
{193

get
{194
return this.m_strMessage;195
}196
}197
198

public override string ToString()
{199
return base.ToString ();200
}201
}202

203
}204

1

/**//*2
* ##############################3
* CreateBy : xuzhong4
* CreateTime : 2003-08-095
* Version : 1.06
* 7
* ModifyTime : 2004-01-068
* Version : 1.19
* ##############################10
* 11
* 12
* Description13
* 14
* version 1.015
* 按登录名查用户16
* 17
* Version 1.118
* 增加按手机号查用户,ADObjectType枚举,查询组19
* 20
* 21
* version 1.222
* 填加常量LDAP_SEARCH_OU_BY_LDAP23
* 24
* 25
*/26
using System;27

28

namespace XZSoftware.ActiveDirectoryLib
{29

/**//// <summary>30
/// LDAP Search Filters String Creator31
/// </summary>32

public sealed class ADLDAPFilterTemplate
{33

34

/**//// <summary>35
/// 通用变量表示字符36
/// </summary>37
public const string LDAP_VALUE = "[value]" ;38

39

/**//// <summary>40
/// 登录名找用户的Filter41
/// </summary>42
public const string LDAP_SEARCH_USER_BY_LOGINNAME = "(&(objectclass=user)(!objectclass=computer)(sAMAccountName=[value]))" ;43
public const string LDAP_SEARCH_USER_BY_CN = "(&(objectclass=user)(!objectclass=computer)(cn=[value]))" ;44
public const string LDAP_SEARCH_USER_BY_EMAIL = "(&(objectclass=user)(!objectclass=computer)(mail=[value]))" ;45

46

1.2#region 1.247

/**//// <summary>48
/// 通过LDAPPath查OU49
/// </summary>50
public const string LDAP_SEARCH_OU_BY_LDAP = "(&(objectclass=organizationalUnit)(adsPath=[value]))";51
#endregion52

53

54
public static string GetFilterByLoginName(string loginName)55

{56
return LDAP_SEARCH_USER_BY_LOGINNAME.Replace(57
LDAP_VALUE ,58
loginName ) ;59
}60

61

/**//// <summary>62
/// 通过cn查找63
/// </summary>64
/// <param name="cnName"></param>65
/// <returns></returns>66
public static string GetFilterByCNName(string cnName)67

{68
return LDAP_SEARCH_USER_BY_CN.Replace(69
LDAP_VALUE ,70
cnName ) ;71
}72

73
public static string GetFilterByEmail(EMailLib.MailAddress mail)74

{75
return LDAP_SEARCH_USER_BY_EMAIL.Replace(76
LDAP_VALUE ,77
mail.ToString() ) ; 78
}79

80

81

82

version 1.1#region version 1.183

84
public const string LDAP_SEARCH_ALL_USERS = "(&(objectclass=user)(!objectclass=computer)(cn=*)(!email=''))";85
public const string LDAP_SEARCH_GROUP_BY_NAME = "((&(objectclass=group)(cn=[value])))";86
public const string LDAP_SEARCH_USER_BY_MOBILE = "(&(objectclass=user)(!objectclass=computer)(mobile=[value]))";87

/**//// <summary>88
/// 通配找用户的Filter89
/// </summary>90
const string strSAMAccountName = "(SAMAccountName=*[value]*)" ;91
const string strObjectClass = "(objectclass=[value])" ;92
const string strCN = "(CN=*[value]*)" ;93
const string strCity = "(l=*[value]*)" ;94
const string strCompany = "(company=*[value]*)" ;95
const string strProvince = "(st=*[value]*)" ;96
const string strHandset = "(telephonenumber=*[value]*)" ;97
const string strName = "(name=*[value]*)" ;98
const string strdisplayname = "(displayname=*[value]*)" ;99
const string strTitle = "(title=*[value]*)" ;100
const string strDepartment = "(department=*[value]*)" ;101
const string strCo = "(co=*[value]*)" ;102
// const string strUserprincipalname = "(userprincipalname=*[value]*)" ;103
const string strMail = "(mail=*[value]*)" ;104
const string strMobile = "(mobile=*[value]*)" ;105
106

/**//// <summary>107
/// 通过手机号查AD用户108
/// </summary>109
public static string GetADUserFilterByMobile(string mobile)110

{111
return LDAP_SEARCH_USER_BY_MOBILE.Replace(112
LDAP_VALUE ,113
mobile ) ;114
}115

116

117

/**//// <summary>118
/// 查询得到有关查询条件的字符串119
/// </summary>120
/// <param name="type">查询类型。</param>121
/// <param name="searchInfo">查询条件,',',',',' '分隔。</param>122
/// <returns>查询字符串</returns>123
public static string GetQueryString(ADObjectType type, string searchInfo)124

{125
string strCondition ;126
string[] strArrCondition ;127
string strHeader = "(|" ;128
string strFooter = ")" ;129
string queryString = "" ;130
string strClass ;131

switch(type)
{132
case ADObjectType.User :133
strClass = "User";134
break;135
case ADObjectType.Computer :136
strClass = "Computer";137
break;138
case ADObjectType.Group :139
strClass = "Group";140
break;141
case ADObjectType.OrganizeUnit :142
strClass = "OU";143
break;144
default:145
strClass = "user";146
break;147
}148
// 如果选择了查询范围149

if ( "" != strClass )
{150
strHeader = "(&" + strObjectClass.Replace( LDAP_VALUE , strClass) + strHeader ;151
strFooter += ")" ;152
}153
strCondition = searchInfo ;154

strArrCondition = strCondition.Split(new char[]
{' ',',',','}) ;155

for ( int i = 0 ; i < strArrCondition.Length ; i ++ )
{156
strArrCondition[i] = strArrCondition[i].Trim() ;157

if ( strArrCondition[i] != "" )
{158
queryString += strCN.Replace(LDAP_VALUE , strArrCondition[i] ) ;159
queryString += strName.Replace(LDAP_VALUE , strArrCondition[i] ) ;160
queryString += strCity.Replace(LDAP_VALUE , strArrCondition[i] ) ;161
queryString += strdisplayname.Replace(LDAP_VALUE , strArrCondition[i] ) ;162
queryString += strProvince.Replace(LDAP_VALUE , strArrCondition[i] ) ;163
queryString += strMail.Replace(LDAP_VALUE , strArrCondition[i] ) ;164

if ( ADObjectType.User == type )
{165
queryString += strSAMAccountName.Replace(LDAP_VALUE , strArrCondition[i] ) ;166
// queryString += strCompany.Replace(LDAP_VALUE , strArrCondition[i] ) ;167
queryString += strHandset.Replace(LDAP_VALUE , strArrCondition[i] ) ;168
// queryString += strTitle.Replace(LDAP_VALUE , strArrCondition[i] ) ;169
queryString += strDepartment.Replace(LDAP_VALUE , strArrCondition[i] ) ;170
// queryString += strCo.Replace(LDAP_VALUE , strArrCondition[i] ) ;171
// 2004-12-1 xuzhong 172
// queryString += strUserprincipalname.Replace(LDAP_VALUE , strArrCondition[i] ) ;173
queryString += strMobile.Replace(LDAP_VALUE , strArrCondition[i] ) ;174
}175
}176
}177
queryString = strHeader + queryString + strFooter ;178
return queryString ;179
}180

181

/**//// <summary>182
/// 得到查询组183
/// </summary>184

public static string GetGroupQueryByName(string groupName)
{185
return LDAP_SEARCH_GROUP_BY_NAME.Replace(186
LDAP_VALUE ,187
groupName ) ;188
}189
#endregion end 1.1190

191
}192

193

#region194

/**//// <summary>195
/// AD中对象的类型196
/// </summary>197

public enum ADObjectType
{198

/**//// <summary>199
/// 用户200
/// </summary>201
User = 0 ,202

/**//// <summary>203
/// 计算机204
/// </summary>205
Computer = 1 ,206

/**//// <summary>207
/// 组208
/// </summary>209
Group = 2 ,210

/**//// <summary>211
/// 组织单位212
/// </summary>213
OrganizeUnit = 3214
}215
#endregion216

217
}218

1

/**//*2
* ############################3
* CreateBy : xuzhong #4
* CreateTime : 2003-08-09 #5
* Version : 1.0 #6
* #7
* ModifyTime : 2003-11-27 #8
* Version : 1.1 #9
* #10
* ModifyTime : 2003-11-27 #11
* Version : 1.2 #12
* #13
* ModifyTime : 2003-12-01 #14
* Version : 1.3 #15
* #16
* ModifyTime : 2004-06-10 #17
* Version : 1.4 #18
* ############################19
* 20
* 21
* Description22
* 23
* version 1.024
* 25
* Version 1.126
* 增加按手机号查用户,获得用户的DirectoryEntry实例27
* 28
* version 1.229
* 实现了IDisposable接口30
* 31
* version 1.332
* 增加新的方法 GetDEByLDAPPath()33
* 34
* version 1.435
* 修改一处bug36
* 37
*/38

39
using System;40
using System.DirectoryServices ;41

42

namespace XZSoftware.ActiveDirectoryLib
{43

/**//// <summary>44
/// ADUtility 提供对AD进入查询,验证,填加等方法。45
/// </summary>46

public class ADUtility : IDisposable , IADUtility
{47
private DirectoryEntry m_directoryEntry ;48

public ADUtility()
{49
}50

51

/**//// <summary>52
/// ADUtility的构造函数53
/// </summary>54
/// <param name="LDAPPath">AD的连接路径</param>55
/// <param name="userName">登录域用户名(最好具有较高的权限)</param>56
/// <param name="password">登录密码</param>57

public ADUtility(string LDAPPath , string userName , string password)
{58
// 得到整个活动目录59
m_directoryEntry =60
new DirectoryEntry(61
LDAPPath,userName,password,AuthenticationTypes.Secure);62
}63

64

/**//// <summary>65
/// 通过用户账号名,得到ADUser对象66
/// </summary>67
/// <param name="loginName">用户登录名</param>68
/// <returns>返回一个ADUser对象</returns>69

public ADUser GetADUser(string loginName)
{70
string userName ;71

if ( loginName.IndexOf("\\") > 0)
{72
// 得到没有域名的登录名。73
userName = loginName.Substring( loginName.IndexOf("\\") + 1) ;74

}else
{75
userName = loginName ;76
}77
string ldapQueryString = ADLDAPFilterTemplate.GetFilterByLoginName( userName ) ;78
ADUser user = new ADUser();79
user.MyDirectoryEntry = (this.GetOnlyOneDEObject( ldapQueryString) ) ;80
return user;81
}82

83

/**//// <summary>84
/// 通来LDAP查询字符串,得到一个(only one)DirectoryEntry对象85
/// </summary>86
/// <param name="ldapQueryString">ldap searcher string</param>87
/// <example>88
/// ADUtility utility= new ADUtility(
);89
/// DirectoryEntry object = utility.GetOnlyOneDEObject("(&(objectclass=user))");90
/// </example>91

public DirectoryEntry GetOnlyOneDEObject(string ldapQueryString)
{92
DirectorySearcher searcher = new DirectorySearcher() ;93
searcher.SearchRoot = this.m_directoryEntry ;94
searcher.Filter = ldapQueryString ;95
searcher.SearchScope = SearchScope.Subtree ;96
// 只找一个对象97
SearchResult sr = searcher.FindOne() ;98

if( sr != null)
{99
return sr.GetDirectoryEntry() ; 100

} else
{101
return null;102
}103
}104

105

106

version 1.2#region version 1.2107

108

/**//// <summary>109
/// 通过手机号查询得到一个ADUser110
/// </summary>111
/// <param name="mobile">手机号</param>112
/// <returns>ADUser</returns>113

public ADUser GetADUserByMobile(string mobile)
{114
string ldapQueryString = ADLDAPFilterTemplate.GetADUserFilterByMobile( mobile ) ;115
ADUser user = new ADUser();116
user.MyDirectoryEntry = (this.GetOnlyOneDEObject( ldapQueryString) ) ;117
return user;118
}119

120

/**//// <summary>121
/// 得到所有的组122
/// </summary>123
/// <returns>ADGroupCollection集合对象</returns>124
/// <remarks>对Activex Directory的操作</remarks>125
/// <example>126
/// ADUtility utility = new ADUtility(
.);127
/// ADGroupCollection = utility.GetAllGroups();128
/// </example>129

public ADGroupCollection GetAllGroups()
{130
ADGroupCollection gc = new ADGroupCollection();131
System.Collections.IList list = this.GetAllDEObject( ADLDAPFilterTemplate.GetGroupQueryByName("*"));132

for(int i = 0 ; i< list.Count ; i++)
{133
gc.Add(new ADGroup((DirectoryEntry)list[i]));134
}135
return gc;136
}137

138

139

/**//// <summary>140
/// 得到查询得到的一个结果集141
/// </summary>142
/// <param name="ldapQueryString">查询字符串</param>143
/// <returns>IList接口实现</returns>144
/// <example>145
/// ADUtility utility = new ADUtility(
.);146
/// System.Collections.IList list = utility.GetAllDEObject("(&(objectclass=user)(cn=*))");147
/// </example>148

public System.Collections.IList GetAllDEObject(string ldapQueryString)
{149
System.Collections.IList list = new System.Collections.ArrayList();150
DirectorySearcher searcher = new DirectorySearcher() ;151
searcher.SearchRoot = this.m_directoryEntry ;152
searcher.Filter = ldapQueryString ;153
searcher.SearchScope = SearchScope.Subtree ;154
// 只找一个对象155
SearchResultCollection sc = searcher.FindAll() ;156

for(int index=0 ; index< sc.Count ; index ++)
{157
list.Add( sc[index].GetDirectoryEntry() );158
}159
return list;160
}161

162

163

/**//// <summary>164
/// 获得所有的用户165
/// </summary>166
/// <returns>ADUserCollection对象</returns>167

public ADUserCollection GetAllUsers()
{168
ADUserCollection users = new ADUserCollection();169
System.Collections.IList list = this.GetAllDEObject( ADLDAPFilterTemplate.GetGroupQueryByName("*"));170

for(int i = 0 ; i< list.Count ; i++)
{171
users.Add(new ADUser((DirectoryEntry)list[i]));172
}173
return users;174
}175
176

177

public void Dispose()
{178
this.m_directoryEntry = null;179
}180

181

182

183

/**//// <summary>184
/// 查询得到要找的用户185
/// </summary>186
/// <param name="queryString">查询内容</param>187
/// <returns>ADUserCollection集合</returns>188

public ADUserCollection GetAllUsers(string queryString)
{189
ADUserCollection users = new ADUserCollection();190
System.Collections.IList list = this.GetAllDEObject(ADLDAPFilterTemplate.GetQueryString(ADObjectType.User,queryString));191

for(int i = 0 ; i< list.Count ; i++)
{192
users.Add(new ADUser((DirectoryEntry)list[i]));193
}194
return users;195
}196

197
#endregion198

199

1.3#region 1.3200

/**//// <summary>201
/// 通过LDAP查询得到一个DirectoryEntry对象202
/// </summary>203
/// <param name="ldapPath">LDAP路径(如:LDAP://nmc.ln.cmcc/DC=nmc,DC=ln,DC=cmcc),你写成DC=nmc,DC=ln,DC=cmcc</param>204
/// <returns>DirectoryEntry对象</returns>205

public DirectoryEntry GetDEByLDAPPath(string ldapPath)
{206

try
{207
return new DirectoryEntry( ADServerLDAP + ldapPath,this.m_directoryEntry.Username,this.m_directoryEntry.Password,AuthenticationTypes.Secure);208

}catch(Exception exc)
{209
throw new ADException (ldapPath + "查询无结果" , exc);210
}211
}212

213
string m_ServerLDAP ;214

/**//// <summary>215
/// AD服务器的LDAP路径(LDAP://xxxxxx/)216
/// </summary>217

public string ADServerLDAP
{218

get
{219
// version 1.4,修改这里的bug ,add m_ServerLDAP == ""220

if (m_ServerLDAP == null || m_ServerLDAP == "")
{221
this.m_ServerLDAP = this.m_directoryEntry.Path.Substring(0,this.m_directoryEntry.Path.IndexOf("/",7)+1);222
}223
return this.m_ServerLDAP ;224
}225
}226
#endregion227
}228

229

public interface IADUtility
{230
ADUser GetADUser(string userName);231
ADUser GetADUserByMobile(string mobile);232
DirectoryEntry GetOnlyOneDEObject(string ldapQueryString);233
ADGroupCollection GetAllGroups();234
System.Collections.IList GetAllDEObject(string ldapQueryString);235
ADUserCollection GetAllUsers();236
ADUserCollection GetAllUsers(string ldapQueryString);237
DirectoryEntry GetDEByLDAPPath(string ldapPath);238

string ADServerLDAP
{get;}239
}240
}241

1

/**//*2
* ##############################3
* # CreateBy : xuzhong #4
* # CreateTime : 2003-08-08 #5
* # Version : 1.1 #6
* # LastModify : 2004-05-28 #7
* # Version : 1.4 #8
* ##############################9
* 10
* #################11
* # Description #12
* #################13
* 14
* version 0.1 :15
* 完成属性DisplayName可读写16
* 17
* version 1.1 :18
* 修改了public ADUser(string loginName),考虑到loginName 可能会有两种情况'nmc\xz' or 'xz'19
* 20
* version 1.2 :21
* 填加ADUserCollection类.22
* ADUser类中加入其它一下属性23
* 24
* version 1.3 :25
* 添加Title字段26
* 27
* version 1.4 :28
* 添加AccountName29
* 30
* version 1.5 :31
* 添加新的属性32
* 33
* 34
* version 1.6:35
* Email 找人36
* 填加部分新的属性,其中有些是自定义字段。37
* 38
* 39
* version 1.7: 2004-05-2840
* 添加省份,城市属性41
* 42
* version 1.8: 2004-06-1043
* 添加用户所属OU属性44
* 45
*/46
using System;47
using System.DirectoryServices ;48

49
namespace XZSoftware.ActiveDirectoryLib 50


{51

/**//// <summary>52
/// 包含活动目录中的用户信息的访问、修改。53
/// </summary>54
public class ADUser:ADObject 55

{56

57
public ADUser() 58

{59
}60

/**//// <summary>61
/// 通过DirectoryEntry对象初始化ADUser62
/// </summary>63
/// <param name="directoryEntry">指定用户的DirectoryEntry对象</param>64
public ADUser(DirectoryEntry directoryEntry):base(directoryEntry)65

{66
if ( directoryEntry == null)67

{68
throw new ADException("传入的DirectoryEntry对象为空。");69
}70
else71

{72
this.MyDirectoryEntry = directoryEntry ;73
}74
}75

76

/**//// <summary>77
/// 通过用户登录账号,初始化ADUser类78
/// </summary>79
/// <param name="loginName">登录名(Server\UserName , or UserName)</param>80
public ADUser(string loginName)81

{82
if (ADObject.Utility != null)83

{84
string userName ;85
if ( loginName.IndexOf("\\") > 0)86

{87
// 得到没有域名的登录名。88
userName = loginName.Substring( loginName.IndexOf("\\") + 1) ;89
}90
else91

{92
userName = loginName ;93
}94
string ldapQueryString = ADLDAPFilterTemplate.GetFilterByLoginName( userName ) ;95
this.MyDirectoryEntry = (ADObject.Utility.GetOnlyOneDEObject( ldapQueryString) ) ;96
if ( this.MyDirectoryEntry == null)97
throw new ADException("无法通过登录名\""+loginName+"\"得到ADUser实体");98
}99
else 100

{101
throw new ADException("必须先给XZSoftware.ActiveDirectoryLib类的静态变量Utility赋值");102
}103
}104

105
public ADUser( char[] cn )106

{107
108
if (ADObject.Utility != null)109

{110
string userName = new string( cn ) ;111
string ldapQueryString = ADLDAPFilterTemplate.GetFilterByCNName( userName ) ;112
this.MyDirectoryEntry = (ADObject.Utility.GetOnlyOneDEObject( ldapQueryString) ) ;113
if ( this.MyDirectoryEntry == null)114
throw new ADException("无法通过CN=\""+userName+"\"得到ADUser实体");115
}116
else 117

{118
throw new ADException("必须先给XZSoftware.ActiveDirectoryLib类的静态变量Utility赋值");119
}120
}121

122

123
public ADUser( EMailLib.MailAddress mail )124

{125
if (ADObject.Utility != null)126

{127
string ldapQueryString = ADLDAPFilterTemplate.GetFilterByEmail( mail ) ;128
this.MyDirectoryEntry = (ADObject.Utility.GetOnlyOneDEObject( ldapQueryString) ) ;129
if ( this.MyDirectoryEntry == null)130
throw new ADException("无法通过Email=\""+mail.ToString()+"\"得到ADUser实体。");131
}132
else 133

{134
throw new ADException("必须先给XZSoftware.ActiveDirectoryLib类的静态变量Utility赋值");135
}136
}137

138

/**//// <summary>139
/// 构造函数140
/// </summary>141
/// <param name="mobileNumber">手机号13

</param>142
public ADUser(ulong mobileNumber)143

{144
if (ADObject.Utility != null)145

{146
string ldapQueryString = ADLDAPFilterTemplate.GetADUserFilterByMobile( mobileNumber.ToString() ) ;147
this.MyDirectoryEntry = (ADObject.Utility.GetOnlyOneDEObject( ldapQueryString) ) ;148
if ( this.MyDirectoryEntry == null)149
throw new ADException("无法通过手机号\""+mobileNumber.ToString()+"\"得到ADUser实体");150
} 151
else 152

{153
throw new ADException("必须先给XZSoftware.ActiveDirectoryLib类的静态变量Utility赋值");154
}155
}156

157

/**//// <summary>158
/// 中文显示名159
/// </summary>160
public string DisplayName 161

{162
get 163

{164
return GetProperty("displayName").ToString() ;165
}166
set 167

{ 168
SetProperty("displayName",value);169
}170
}171

172

173

/**//// <summary>174
/// 描述(生日)175
/// </summary>176
public string Description 177

{178
get 179

{180
return GetProperty("Description").ToString() ;181
}182
set 183

{ 184
SetProperty("Description",value);185
}186
}187

188

/**//// <summary>189
/// 手机号190
/// </summary>191
public string Mobile 192

{193
get 194

{195
return GetProperty("Mobile").ToString() ;196
}197
set 198

{ 199
SetProperty("Mobile",value);200
}201
}202

203

/**//// <summary>204
/// 性别(这是一个新添字段,在别的地方用不了)205
/// </summary>206
public bool Sex 207

{208
get 209

{210
return Convert.ToBoolean(GetProperty("sex")) ;211
}212
set 213

{ 214
SetProperty("sex",value);215
}216
}217

218

/**//// <summary>219
/// 获得CN值(只读)220
/// </summary>221
public string CN 222

{223
get 224

{225
return GetProperty("cn").ToString() ;226
}227
}228

229

/**//// <summary>230
/// 传呼机号231
/// </summary>232
public string Pager233

{234
get235

{236
return GetProperty("pager").ToString();237
}238
set239

{240
SetProperty("pager",value);241
}242
}243

244

/**//// <summary>245
/// 所在的组246
/// </summary>247
public ADGroupCollection MemberOf248

{249
get250

{251
ADGroupCollection adGroupCollection = new ADGroupCollection();252
System.Collections.IList groupList = this.GetProperties("MemberOf");253

254
if(groupList!=null)255

{256
foreach( object adGroupPath in groupList )257

{258
string groupPath = ""; 259
string groupName = "";260
261
// 如:CN=全省传输人员组,OU=专业人员,OU=应用系统,DC=nmc262
groupPath = adGroupPath.ToString();263

264
// 要得到"全省传输人员组"265
int groupNameLength = groupPath.IndexOf(",",0) - 3; // Get the group's name length266
groupName = groupPath.Substring( 3,groupNameLength );267

268
// Create a group object269
ADGroup group = new ADGroup( groupName );270
adGroupCollection.Add( group );271
}272
}273

274
return adGroupCollection;275
}276
}277

278

version 1.2#region version 1.2279

/**//// <summary>280
/// 邮箱281
/// </summary>282
public string EMail283

{284

get
{return GetProperty("mail").ToString();}285

set
{ this.SetProperty("mail",value);}286
}287

288

/**//// <summary>289
/// 国家290
/// </summary>291
public string Country292

{293

get
{return GetProperty("c").ToString();}294
}295

296
#endregion297

298

299

1.3#region 1.3300
public string Title301

{302
get303

{304
return GetProperty("Title").ToString();305
}306
set 307

{308
SetProperty("Title",value);309
}310
}311
#endregion312

313

314

1.4#region 1.4315

316

/**//// <summary>317
/// 登录名318
/// </summary>319
public string AccountName320

{321
get322

{323
return GetProperty("SAMAccountName").ToString();324
}325
}326

327

/**//// <summary>328
/// 注释329
/// </summary>330
public string Info331

{332
get333

{334
return GetProperty("Info").ToString();335
}336
set 337

{338
SetProperty("Info",value);339
}340
}341
342
#endregion343

344

345

1.5#region 1.5346

/**//// <summary>347
/// Specifies flags that control password, lockout, disable/enable, script, and home directory behavior for the user. This property also contains a flag that indicates the account type of the object. The flags are defined in UserAccountControlType<br>348
/// 66048;启用账号,546为禁用349
/// </summary>350
public int UserAccountControl351

{352
get353

{354
return (int)this.GetProperty("userAccountControl");355
}356
set357

{358
this.SetProperty("userAccountControl" , value);359
}360
}361

362

/**//// <summary>363
/// 邮政编码364
/// </summary>365
public string PostalCode366

{367
get368

{369
return this.GetProperty("postalCode").ToString();370
}371
set372

{373
this.SetProperty("postalCode", value);374
}375
}376
#endregion377

378

379

380

1.6#region 1.6381

382

/**//// <summary>383
/// 别名(昵称)384
/// </summary>385
public string MailNickName386

{387
get388

{389
return this.GetProperty("mailNickname").ToString();390
}391
set392

{393
this.SetProperty("mailNickname", value);394
}395
}396

397

/**//// <summary>398
/// 生日399
/// </summary>400
public string Birthday401

{402
get403

{404
return this.GetProperty("Birthday").ToString();405
}406
set407

{408
this.SetProperty("Birthday", value);409
}410
}411

412

413

/**//// <summary>414
/// 学历415
/// </summary>416
public string Degree417

{418
get419

{420
return this.GetProperty("Degree").ToString();421
}422
set423

{424
this.SetProperty("Degree", value);425
}426
}427

428

429

/**//// <summary>430
/// IP电话431
/// </summary>432
public string IPPhone433

{434
get435

{436
return this.GetProperty("ipPhone").ToString();437
}438
set439

{440
this.SetProperty("ipPhone", value);441
}442
}443

444

445

/**//// <summary>446
/// 部门447
/// </summary>448
public string Department449

{450
get451

{452
return this.GetProperty("department").ToString();453
}454
set455

{456
this.SetProperty("department", value);457
}458
}459

460

/**//// <summary>461
/// 公司462
/// </summary>463
public string Company464

{465
get466

{467
return this.GetProperty("company").ToString();468
}469
set470

{471
this.SetProperty("company", value);472
}473
}474

475

476

/**//// <summary>477
/// 管理者(领导)478
/// </summary>479
public ADUser Manager480

{481
get482

{483
System.DirectoryServices.DirectoryEntry entry ;484
ADUser user ;485
string managerPath ;486
try487

{488
managerPath = this.GetProperty("Manager").ToString();489
entry = ADObject.Utility.GetDEByLDAPPath( managerPath );490
user = new ADUser( entry );491
}492
catch493

{494
user = null;495
}496
return user;497
}498
}499

500

501

/**//// <summary>502
/// 街道503
/// </summary>504
public string Street505

{506
get507

{508
return this.GetProperty("Street").ToString();509
}510
set511

{512
this.SetProperty("Street", value);513
}514
}515

516

/**//// <summary>517
/// 地址518
/// </summary>519
public string StreetAddress520

{521
get522

{523
return this.GetProperty("StreetAddress").ToString();524
}525
set526

{527
this.SetProperty("StreetAddress", value);528
}529
}530

531

532

/**//// <summary>533
/// 住宅电话534
/// </summary>535
public string HomePhone536

{537
get538

{539
return this.GetProperty("HomePhone").ToString();540
}541
set542

{543
this.SetProperty("HomePhone", value);544
}545
}546

547

548

/**//// <summary>549
/// 主页550
/// </summary>551
public string WWWHomePage552

{553
get554

{555
return this.GetProperty("wWWHomePage").ToString();556
}557
set558

{559
this.SetProperty("wWWHomePage", value);560
}561
}562

563

564

/**//// <summary>565
/// Fax566
/// </summary>567
public string FacsimileTelephoneNumber568

{569
get570

{571
return this.GetProperty("FacsimileTelephoneNumber").ToString();572
}573
set574

{575
this.SetProperty("FacsimileTelephoneNumber", value);576
}577
}578

579

/**//// <summary>580
/// 国家581
/// </summary>582
public string C583

{584
get585

{586
return this.GetProperty("C").ToString();587
}588
set589

{590
this.SetProperty("C", value);591
}592
}593

594

/**//// <summary>595
/// 国家代码596
/// </summary>597
public int CountryCode598

{599
get600

{601
return Convert.ToInt32(this.GetProperty("CountryCode"));602
}603
set604

{605
this.SetProperty("CountryCode", value);606
}607
}608

609

610

/**//// <summary>611
/// 名612
/// </summary>613
public string GivenName614

{615
get616

{617
return this.GetProperty("givenName").ToString();618
}619
set620

{621
this.SetProperty("givenName", value);622
}623
}624

625

626
public int LogonCount627

{628
get629

{630
return Convert.ToInt32(this.GetProperty("LogonCount"));631
}632
set633

{634
this.SetProperty("LogonCount", value);635
}636
}637

638

639
public bool Married640

{641
get642

{643
return Convert.ToBoolean(this.GetProperty("Married"));644
}645
set646

{647
this.SetProperty("Married", value);648
}649
}650

651

652

/**//// <summary>653
/// 办公电话654
/// </summary>655
public string TelephoneNumber656

{657
get658

{659
return this.GetProperty("TelephoneNumber").ToString();660
}661
set662

{663
this.SetProperty("TelephoneNumber", value);664
}665
}666

667

/**//// <summary>668
/// 办公地址669
/// </summary>670
public string PhysicalDeliveryOfficeName671

{672
get673

{674
return this.GetProperty("PhysicalDeliveryOfficeName").ToString();675
}676
set677

{678
this.SetProperty("PhysicalDeliveryOfficeName", value);679
}680
}681

682

683

/**//// <summary>684
/// 下属685
/// </summary>686
public ADUserCollection DirectReports687

{688
get689

{690
string userPath;691
ADUser userObj;692
ADUserCollection uc;693
System.DirectoryServices.DirectoryEntry entryObj;694
System.Collections.IList list;695

696
uc = new ADUserCollection();697
try698

{699
list = this.GetProperties("DirectReports");700
int count = list.Count;701
for(int i=0 ; i<count ; i++)702

{703
userPath = Convert.ToString(list[i]);704
entryObj = ADObject.Utility.GetDEByLDAPPath( userPath );705
userObj = new ADUser( entryObj );706
uc.Add( userObj );707
}708
}709
catch710

{711
}712
return uc;713
714
}715
}716
#endregion717

718

719

1.7#region 1.7720

721

/**//// <summary>722
/// 用户AD中的城市信息723
/// </summary>724
public string City725

{726
get727

{728
return this.GetProperty("l").ToString();729
}730
}731

732
#endregion733

734

735

736

1.8#region 1.8737
private ADOrganizeUnit _ou;738
public ADOrganizeUnit OU739

{740
get741

{742
if ( _ou == null )743

{744
string adsPath = this.AdsPath.ToUpper().Replace("CN="+this.CN.ToUpper()+",","");745
ADOrganizeUnit ou = new ADOrganizeUnit( adsPath );746
_ou = ou ;747
}748
return _ou ;749
}750
}751
#endregion752
}753

754

755

/**//// <summary>756
/// ADUser集合类757
/// </summary>758
public class ADUserCollection : System.Collections.CollectionBase759

{760

761

局部变量#region 局部变量762
bool _blnIsReadOnly = false;763
#endregion764

765

/**//// <summary>766
/// 构造函数767
/// </summary>768

public ADUserCollection ()
{}769

770

/**//// <summary>771
/// 返回ADUser对象772
/// </summary>773
public ADUser this[int index] 774

{775
get 776

{777
return (ADUser)this.List[index];778
}779
set 780

{781
this.List.Add( value );782
}783
}784

785

786

/**//// <summary>787
/// 填加ADUser对象788
/// </summary>789
/// <param name="value">ADUser实体</param>790
/// <returns>它所在的位置</returns>791
public int Add(ADUser value) 792

{793
if(!this._blnIsReadOnly)794

{ // 如果可写795
return this.List.Add(value);796
}797
else798

{799
throw new Exception("对象被写保护");800
}801
}802
803

/**//// <summary>804
/// 是否可写(false:可写,true:不可写)805
/// </summary>806
public bool IsReadOnly 807

{808
get 809

{810
return this._blnIsReadOnly;811
}812
}813
}814

815

816

/**//// <summary>817
/// Specifies flags that control password, lockout, disable/enable, script, and home directory behavior for the user. This property also contains a flag that indicates the account type of the object. The flags are defined in LMACCESS.H818
/// </summary>819
public enum UserAccountControlType : int820

{821
// 4 bytes. 822
UF_SCRIPT = 0x000001, 823
UF_ACCOUNTDISABLE = 0x000002, 824
UF_HOMEDIR_REQUIRED = 0x000008, 825
UF_LOCKOUT = 0x000010, 826
UF_PASSWD_NOTREQD = 0x000020, 827
UF_PASSWD_CANT_CHANGE = 0x000040,828
UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 0x000080,829

/**//// <summary>830
/// This is an account for users whose primary account is in another domain. This account provides user access to this domain, but not to any domain that trusts this domain. The User Manager refers to this account type as a local user account. 831
/// </summary>832
UF_TEMP_DUPLICATE_ACCOUNT = 0x000100, 833

/**//// <summary>834
/// This is a default account type that represents a typical user835
/// </summary>836
UF_NORMAL_ACCOUNT = 0x000200,837

/**//// <summary>838
/// This is a permit to trust account for a Windows NT domain that trusts other domains. 839
/// </summary>840
UF_INTERDOMAIN_TRUST_ACCOUNT = 0x000800,841

/**//// <summary>842
/// This is a computer account for a Windows NT Workstation/Windows 2000 Professional or Windows NT Server/Windows 2000 Server that is a member of this domain.843
/// </summary>844
UF_WORKSTATION_TRUST_ACCOUNT = 0x001000,845

/**//// <summary>846
/// This is a computer account for a Windows NT Backup Domain Controller that is a member of this domain. 847
/// </summary>848
UF_SERVER_TRUST_ACCOUNT = 0x002000,849
UF_MACHINE_ACCOUNT_MASK = UF_INTERDOMAIN_TRUST_ACCOUNT | UF_WORKSTATION_TRUST_ACCOUNT | UF_SERVER_TRUST_ACCOUNT,850
UF_ACCOUNT_TYPE_MASK = UF_TEMP_DUPLICATE_ACCOUNT | UF_NORMAL_ACCOUNT | UF_INTERDOMAIN_TRUST_ACCOUNT | UF_WORKSTATION_TRUST_ACCOUNT | UF_SERVER_TRUST_ACCOUNT,851
UF_DONT_EXPIRE_PASSWD = 0x010000,852
UF_MNS_LOGON_ACCOUNT = 0x020000,853
UF_SMARTCARD_REQUIRED = 0x040000,854
UF_TRUSTED_FOR_DELEGATION = 0x080000,855
UF_NOT_DELEGATED = 0x100000,856
UF_USE_DES_KEY_ONLY = 0x200000,857
UF_DONT_REQUIRE_PREAUTH = 0x400000,858
UF_PASSWORD_EXPIRED = 0x800000,859
UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 0x1000000,860
UF_SETTABLE_BITS = UF_SCRIPT | UF_ACCOUNTDISABLE | UF_LOCKOUT | UF_HOMEDIR_REQUIRED | UF_PASSWD_NOTREQD | UF_PASSWD_CANT_CHANGE | UF_ACCOUNT_TYPE_MASK | UF_DONT_EXPIRE_PASSWD | UF_MNS_LOGON_ACCOUNT | UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED | UF_SMARTCARD_REQUIRED | UF_TRUSTED_FOR_DELEGATION | UF_NOT_DELEGATED | UF_USE_DES_KEY_ONLY | UF_DONT_REQUIRE_PREAUTH |UF_PASSWORD_EXPIRED | UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION861
}862
}863

浙公网安备 33010602011771号