📄 adservices.cs
字号:
//从组中删除用户
try
{
objGroupExsited = new DirectoryEntry(strGroupPath);
objGroupExsited.Invoke("Remove", new Object[] {strUserPath});
objGroupExsited.Close();
objGroupExsited.Dispose();
}
catch(Exception err)
{
strErrDescription = err.ToString();
return false;
}
}
return true;
}
catch(Exception err)
{
strErrDescription = err.ToString();
return false;
}
}
/// <summary>
/// 从组中删除用户名
/// </summary>
/// <param name="strUserName">用户名</param>
/// <param name="strGroup">组名</param>
/// <returns>bool</returns>
bool IUser.DeleteUserFormGroup(string[] strUserName, string strGroup)
{
IUser myUser = (IUser)new ADServices();
return myUser.DeleteUserFormGroup(CONST_ADSPATH,strUserName,strGroup);
}
#endregion
#region[修改密码]
bool IUser.ChangePassword(string strUsername,string oldPassword,string newPassword,string strDomain)
{
//检查是否存在此用户,及检查
ILogonNTUser objChangePassword = (ILogonNTUser)new ADServices();
if (!objChangePassword.LogonNTUser(strUsername,oldPassword,strDomain))
{
strErrDescription = "Error Desciption:user no fond or old password is error,please again";
return false;
}
IQueryList objGetPath = (IQueryList)new ADServices();
string strGetUserPath = objGetPath.GetObjPath(strUsername,QueryType.UserName)[0];
myEntry = new DirectoryEntry(strGetUserPath);
try
{
myEntry.Invoke("SetPassword", new object[] {newPassword});
myEntry.Close();
myEntry.Dispose();
return true;
}
catch(Exception err)
{
strErrDescription = err.ToString();
return false;
}
}
bool IUser.ChangePassword(string strUsername,string oldPassword,string newPassword)
{
IUser objChangePassword = (IUser)new ADServices();
return objChangePassword.ChangePassword(strUsername,oldPassword,newPassword,CONST_DOMAIN);
}
#endregion
#region[用户的基本信息]
structUserNameBasicInfo IQueryList.GetUserNameBasicInfo(string strUserName)
{
IQueryList objQuery = (IQueryList)new ADServices();
structUserNameBasicInfo basicInfo = new structUserNameBasicInfo();
try
{
string strUserPath = objQuery.GetObjPath(strUserName,QueryType.UserName)[0];
myEntry = new DirectoryEntry(strUserPath);
basicInfo.Cn = myEntry.Properties["cn"][0].ToString();
basicInfo.Company = myEntry.Properties["Company"][0].ToString();
basicInfo.Department = myEntry.Properties["department"][0].ToString();
basicInfo.Employeeid = myEntry.Properties["employeeid"][0].ToString();
basicInfo.FirstName = myEntry.Properties["sn"][0].ToString();
basicInfo.LastName = myEntry.Properties["givenName"][0].ToString();
basicInfo.Mail = myEntry.Properties["mail"][0].ToString();
basicInfo.NTAccount = myEntry.Properties["SAMAccountName"][0].ToString();
basicInfo.TelephoneNumber = myEntry.Properties["telephoneNumber"][0].ToString();
basicInfo.Title = myEntry.Properties["title"][0].ToString();
basicInfo.WhenChange = myEntry.Properties["whenchanged"][0].ToString();
basicInfo.WhenCreated = myEntry.Properties["whencreated"][0].ToString();
//管理人员
int max = myEntry.Properties["manager"].Count;
basicInfo.Manager = new string[max];
for(int i=0;i<=max-1;i++)
{
basicInfo.Manager[i] = myEntry.Properties["manager"][i].ToString();
}
//所属组名
int _max = myEntry.Properties["memberof"].Count;
basicInfo.MemberOf = new string[_max];
for(int i=0;i<=_max-1;i++)
{
basicInfo.MemberOf[i] = objQuery.GetUserGroupMembershipArr(strUserName)[i].ToString();
}
myEntry.Close();
myEntry.Dispose();
}
catch(Exception err)
{
strErrDescription = err.ToString();
}
return basicInfo;
}
#endregion
#region[部门清单]
/// <summary>
/// 得到部门的清单[带有公司的LDAP//地址]
/// </summary>
/// <param name="CompanyADSPath">公司的ADSPath [例如 SD公司的地址:LDAP://gfg1.esquel.com/OU=SD,OU=Organization,DC=gfg1,DC=esquel,DC=com]</param>
/// <returns>返回DataView</returns>
string DeptFilter = "not name in ('Users','Other','Computers','User',' GaoMing Internet Users Address','EAP','EEL','GEG','GEK','GES','GEW','Recipients','SD','Taining School')";
DataView IQueryList.GetDeptList(string CompanyADSPath)
{
GetOUList(CompanyADSPath,strFilter,strPropertiesToLoad,SearchScope.Subtree);
NewTable(mySearcher);
myView = myTable.DefaultView;
myView.RowFilter = DeptFilter;
myView.Sort = "name";
myEntry.Close();
mySearcher.Dispose();
return myView;
}
/// <summary>
/// 得到所有部门的清单
/// </summary>
/// <returns>DataView 字段有[name][ADsPath]</returns>
DataView IQueryList.GetDeptList()
{
IQueryList Ilist = new ADServices();
myView = Ilist.GetCompanyList();
myTable = myView.Table.Clone();
//填充数据
foreach (DataRow iRow in myView.Table.Rows)
{
CloneData(ref myTable,Ilist.GetDeptList(iRow["ADsPath"].ToString()).Table);
}
myView = myTable.DefaultView;
myView.RowFilter = DeptFilter;
myView.Sort = "name";
return myView;
}
#endregion
#region[公共方法]
private object GetNTAccount(string ADsPath)
{
strBuilder = new StringBuilder();
strBuilder.Append(ADsPath);
strBuilder.Replace("/","\\/");
Trace.WriteLine(strBuilder);
DirectoryEntry rootEntry = new DirectoryEntry("LDAP://" + strBuilder);
System.DirectoryServices.PropertyCollection userProps = rootEntry.Properties;
object obVal = userProps["sAMAccountName"].Value;
rootEntry.Close();
rootEntry.Dispose();
return obVal;
}
/// <summary>
/// 得到组织单元[OU]
/// </summary>
/// <param name="ADsPath">LDAP://路径</param>
private void GetOUList(string ADsPath,string strFilter,string strPropertiesToLoad,SearchScope Scope)
{
myEntry = new DirectoryEntry(ADsPath);
mySearcher = new DirectorySearcher(myEntry);
mySearcher.Filter = strFilter;
mySearcher.SearchScope = Scope;
mySearcher.PageSize =100;
mySearcher.SizeLimit= 900;
char[] delimiter = ",".ToCharArray();
string[] PropertiesToLoad = strPropertiesToLoad.Split(delimiter);
mySearcher.PropertiesToLoad.AddRange(PropertiesToLoad);
}
/// <summary>
/// 拷贝数据
/// </summary>
/// <param name="ToTable">合并后的数据集</param>
/// <param name="FormTable">被全并的数据集</param>
private void CloneData(ref DataTable ToTable,DataTable FormTable)
{
if(FormTable.Columns.Count<=0)
return;
//创建列名
if(ToTable.Columns.Count<=0)
{
foreach (DataColumn FormCol in FormTable.Columns)
{
ToTable.Columns.Add(FormCol.ToString(),FormCol.GetType());
}
}
DataRow myRow;
//填充数据
foreach(DataRow FormRow in FormTable.Rows)
{
myRow = ToTable.NewRow();
for(int i=0;i<=ToTable.Columns.Count-1;i++)
{
myRow[i] = FormRow[i];
}
ToTable.Rows.Add(myRow);
}
}
/// <summary>
/// 新建一张表
/// </summary>
/// <param name="mySearcher"></param>
private void NewTable(DirectorySearcher mySearcher)
{
myTable = new DataTable();
//新建列名
foreach(string colName in mySearcher.PropertiesToLoad)
myTable.Columns.Add(colName,Type.GetType ("System.String"));
//填充数据
foreach(SearchResult res in mySearcher.FindAll())
{
myRow= myTable.NewRow();
foreach(string colName in mySearcher.PropertiesToLoad)
{
strBuilder = new StringBuilder();
if(res.Properties.Contains(colName))
{
if(colName == "managedBy")
{
object obVal = GetNTAccount(res.Properties[colName][0].ToString());
myRow[colName] = obVal.ToString();
}
else
myRow[colName] = res.Properties[colName][0].ToString();
}
else
myRow[colName] = "";
}
myTable.Rows.Add(myRow);
}
}
/// <summary>
/// 用户是否存在
/// </summary>
/// <param name="myEntry"></param>
/// <param name="strUsername"></param>
/// <returns>ADsPath</returns>
private string UserPath(DirectoryEntry myEntry,string strUsername)
{
mySearcher = new DirectorySearcher(myEntry);
mySearcher.Filter = ("(sAMAccountName="+strUsername+")");
SearchResult res = mySearcher.FindOne();
if(res == null)
return "";
else
return res.Path;
}
/// <summary>
/// 组名是否存在
/// </summary>
/// <param name="myEntry"></param>
/// <param name="strGroup"></param>
/// <returns>ADsPath</returns>
private string GroupPath(DirectoryEntry myEntry,string strGroup)
{
DirectorySearcher _mySearcher = new DirectorySearcher(myEntry);
_mySearcher.Filter = ("(&(objectClass=Group)(name="+strGroup+"))");
SearchResult _res = _mySearcher.FindOne();
if(_res == null)
return "";
else
return _res.Path;
}
#endregion
}
#region[自定义类型]
//枚举类型
public enum QueryType
{
UserName,GroupName,ComputerName,OUName
}
//结构类型
public struct structUserNameBasicInfo
{
public string[] Manager;
public string[] MemberOf;
public string Cn;
public string Company;
public string Department;
public string Mail;
public string Employeeid;
public string FirstName;
public string LastName;
public string NTAccount;
public string TelephoneNumber;
public string Title;
public string WhenChange;
public string WhenCreated;
}
#endregion
#region [发布接口]
/// <summary>
/// 用户验证
/// </summary>
public interface ILogonNTUser
{
bool LogonNTUser(string UserName,string Password,string Domain);
bool LogonNTUser(string UserName,string Password);
}
/// <summary>
/// 查询
/// </summary>
public interface IQueryList
{ //异常属性
string ExceptionDesc{get;}
//公司清单
DataView GetCompanyList();
StringCollection GetCompanyArr();
//用户组清单
StringCollection GetUserGroupMembershipArr(string strUser);
DataView GetUserGroupMembershipList(string strUser);
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
StringCollection GetUserGroupMembershipArr(string strDomain,string strUser);
DataView GetUserGroupMembershipList(string strDomain, string strUser);
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//帐号清单
DataView GetAccountsList();
DataView GetAccountsList(string ADSPath);
StringCollection GetAccountsArr();
StringCollection GetAccountsArr(string ADSPath);
//电脑清单
DataView GetComputerList(string ADSPath);
//组的成员
StringCollection GetGroupMembersArr(string strDomain, string strGroup);
StringCollection GetGroupMembersArr(string strGroup);
//组的清单
DataView GetGroupList(string ADSPath);
StringCollection GetGroupArr(string ADSPath);
//部门清单
DataView GetDeptList(string CompanyADSPath);
DataView GetDeptList();
//取出对象的地址
StringCollection GetObjPath(string ADsPath,string adName,QueryType enumType);
StringCollection GetObjPath(string adName,QueryType enumType);
//取出对象的属性
StringCollection GetObjAttribute(string ADsPath,string adName,QueryType enumType,bool isShowValue);
StringCollection GetObjAttribute(string adName,QueryType enumType,bool isShowValue);
StringCollection GetObjAttribute(string adName,QueryType enumType);
StringCollection GetObjAttribute(string ADsPath,string adName,QueryType enumType);
StringCollection GetObjAttribute(string ADsPath,bool isShowValue);
StringCollection GetObjAttribute(string ADsPath);
//取出用户的基本信息
structUserNameBasicInfo GetUserNameBasicInfo(string strUserName);
}
public interface IGroup
{
//异常属性
string ExceptionDesc{get;}
//新增一个组
bool AddGroup(string ADSPath, string strGroupName, string strDescription, bool isOverWrite);
bool AddGroup(string ADSPath, string strGroupName, string strDescription);
bool AddGroup(string ADSPath, string strGroupName, bool isOverWrite);
bool AddGroup(string ADSPath, string strGroupName);
//删除一个组
bool DeleteGroup(string strGroupName);
bool DeleteGroup(string ADSPath,string strGroupName);
}
public interface IUser
{
string ExceptionDesc{get;}
//新增一个用户
bool AddUser(string ADSPath, string strUsername, string strPassword,string FirstName,string LastName);
bool AddUser(string ADSPath, string strUsername, string strPassword);
//新增一个用户到组
bool AddUserToGroup(string strUsername,string strGroup);
bool AddUserToGroup(string[] strUserName,string strGroup);
//删除一个用户
bool DeleteUser(string ADsPath,string strUserName);
bool DeleteUser(string ADsPath,string[] strUserName);
bool DeleteUser(string strUserName);
bool DeleteUser(string[] strUserName);
//删除一个用户从组中
bool DeleteUserFormGroup(string ADsPath,string strUserName, string strGroup);
bool DeleteUserFormGroup(string strUserName, string strGroup);
bool DeleteUserFormGroup(string ADsPath,string[] strUserName, string strGroup);
bool DeleteUserFormGroup(string[] strUserName, string strGroup);
//修改密码
bool ChangePassword(string strUsername,string oldPassword,string newPassword,string strDomain);
bool ChangePassword(string strUsername,string oldPassword,string newPassword);
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -