📄 adservices.cs
字号:
/// <param name="strPath">新创建组的地址 [例如:LDAP://gfg1.esquel.com/OU=Organization,DC=gfg1,DC=esquel,DC=com]</param>
/// <param name="strGroupName">新创建组的名字</param>
/// <param name="strDescription">新创建组的描述</param>
/// <returns>true|false true"组创建成功",false"组创建失败"</returns>
bool IGroup.AddGroup(string strPath, string strGroupName, string strDescription)
{
IGroup myGroup = (IGroup)new ADServices();
return myGroup.AddGroup(strPath,strGroupName,strDescription,CONST_ISOVERWRITE);
}
/// <summary>
/// 创建新组
/// </summary>
/// <param name="strPath">新创建组的地址 [例如:LDAP://gfg1.esquel.com/OU=Organization,DC=gfg1,DC=esquel,DC=com]</param>
/// <param name="strGroupName">新创建组的名字</param>
/// <param name="isOverWrite">true|fase "true"删组相同的组,"false"不删除返回false</param>
/// <returns>true|false true"组创建成功",false"组创建失败"</returns>
bool IGroup.AddGroup(string strPath, string strGroupName,bool isOverWrite)
{
IGroup myGroup = (IGroup)new ADServices();
return myGroup.AddGroup(strPath,strGroupName,CONST_STRDESCRIPTION,isOverWrite);
}
/// <summary>
/// 创建新组
/// </summary>
/// <param name="strPath">新创建组的地址 [例如:LDAP://gfg1.esquel.com/OU=Organization,DC=gfg1,DC=esquel,DC=com]</param>
/// <param name="strGroupName">新创建组的名字</param>
/// <param name="strGroupName">新创建组的名字</param>
/// <returns>true|false true"组创建成功",false"组创建失败"</returns>
bool IGroup.AddGroup(string strPath, string strGroupName)
{
IGroup myGroup = (IGroup)new ADServices();
return myGroup.AddGroup(strPath,strGroupName,CONST_ISOVERWRITE);
}
#endregion
#region[删除组]
/// <summary>
/// 删除已有的组名
/// </summary>
/// <param name="strPath">被删除组的地址 [例如:LDAP://gfg1.esquel.com/OU=Organization,DC=gfg1,DC=esquel,DC=com]</param>
/// <param name="strGroupName">被删除组的名字</param>
/// <returns>true|false "true"删除组成功","false"删除组失败</returns>
bool IGroup.DeleteGroup(string strPath,string strGroupName)
{
try
{
myEntry = new DirectoryEntry(strPath);
DirectoryEntry myEntryF = myEntry.Children.Find(strGroupName,"group");
myEntryF.Children.Remove(myEntryF);
return true;
}
catch(Exception err)
{
strErrDescription = err.ToString();
return false;
}
}
/// <summary>
/// 删除已有的组名
/// </summary>
/// <param name="strGroupName">被删除组的名字</param>
/// <returns>true|false "true"删除组成功","false"删除组失败</returns>
bool IGroup.DeleteGroup(string strGroupName)
{
IGroup myGroup = (IGroup)new ADServices();
return myGroup.DeleteGroup(CONST_ADSPATH,strGroupName);
}
#endregion
#region[新增用户]
/// <summary>
/// 新增用户
/// </summary>
/// <param name="ADSPath">LDAP://路径</param>
/// <param name="strUsername">用户名</param>
/// <param name="strPassword">密码</param>
/// <param name="strFullname">中文名</param>
/// <returns>bool</returns>
bool IUser.AddUser(string ADSPath, string strUsername, string strPassword,string FirstName,string LastName)
{
DirectoryEntry NewUser = null;
myEntry = new DirectoryEntry(ADSPath);
myEntries = myEntry.Children;
NewUser = myEntries.Add(strUsername,"User");
try
{
NewUser.Properties["samAccountName"].Add(strUsername);
NewUser.Properties["sn"].Add(FirstName);
NewUser.Properties["givenName"].Add(LastName);
NewUser.Properties["c"].Add("CN");
NewUser.Properties["l"].Add("Gao Ming");
NewUser.Properties["co"].Add("CHINA");
NewUser.Properties["st"].Add("Guang Dong");
NewUser.Properties["postalCode"].Add("528500");
NewUser.CommitChanges();
}
catch (Exception err)
{
strErrDescription = err.ToString();
return false;
}
try
{
NewUser.Invoke("SetPassword", new object[] {strPassword});
NewUser.CommitChanges();
}
catch(Exception err)
{
strErrDescription = err.ToString();
return false;
}
NewUser.Close();
NewUser.Dispose();
return true;
}
/// <summary>
/// 新增用户
/// </summary>
/// <param name="ADSPath">LDAP://路径</param>
/// <param name="strUsername">用户名</param>
/// <param name="strPassword">密码</param>
/// <returns>bool</returns>
bool IUser.AddUser(string ADSPath, string strUsername, string strPassword)
{
IUser myUser = (IUser)new ADServices();
return myUser.AddUser(ADSPath,strUsername,strPassword,"","");
}
/// <summary>
/// 新增用户到组
/// </summary>
/// <param name="strUsername">用户名</param>
/// <param name="strGroup">组名</param>
/// <returns></returns>
bool IUser.AddUserToGroup(string strUsername,string strGroup)
{
DirectoryEntry objGroupExsited;
myEntry = new DirectoryEntry(CONST_ADSPATH);
string strUserPath= UserPath(myEntry,strUsername);
string strGroupPath = GroupPath(myEntry,strGroup);
//查询用户名是否存在
if(strUserPath == "")
{
strErrDescription = "UserName: "+strUsername+" - no fond.";
return false;
}
//查找组名是否存在
if(strGroupPath=="")
{
strErrDescription = "GroupName: "+strGroup+" - no fond.";
return false;
}
//加入用户
try
{
objGroupExsited = new DirectoryEntry(strGroupPath);
objGroupExsited.Invoke("Add", new Object[] {strUserPath});
objGroupExsited.Close();
objGroupExsited.Dispose();
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.AddUserToGroup(string[] strUserName,string strGroup)
{
DirectoryEntry objGroupExsited;
myEntry = new DirectoryEntry(CONST_ADSPATH);
try
{
//判断用户是否存在
foreach(string strUser in strUserName)
{
string strUserPath= UserPath(myEntry,strUser);
if( strUserPath == "")
{
strErrDescription = "UserName: "+strUser+" - no fond.";
return false;
}
}
foreach(string strUser in strUserName)
{
string strUserPath= UserPath(myEntry,strUser);
string strGroupPath = GroupPath(myEntry,strGroup);
//把户用加入组中
try
{
objGroupExsited = new DirectoryEntry(strGroupPath);
objGroupExsited.Invoke("Add", 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></returns>
#endregion
#region[删除用户]
/// <summary>
/// 删除用户
/// </summary>
/// <param name="strUserName">用户名</param>
/// <returns>bool</returns>
bool IUser.DeleteUser(string strUserName)
{
IUser myUser = (IUser)new ADServices();
return myUser.DeleteUser(CONST_ADSPATH,strUserName);
}
/// <summary>
/// 删除用户
/// </summary>
/// <param name="ADsPath">LDAP://路径</param>
/// <param name="strUserName">用户名</param>
/// <returns>bool</returns>
bool IUser.DeleteUser(string ADsPath,string strUserName)
{
myEntry = new DirectoryEntry(ADsPath);
DirectoryEntry objDelEntry;
string strUserPath= UserPath(myEntry,strUserName);
//查询用户名是否存在
if(strUserPath == "")
{
strErrDescription = "UserName: "+strUserName+" - no fond.";
return false;
}
//删除一个用户
try
{
objDelEntry = new DirectoryEntry(strUserPath);
objDelEntry.Parent.Children.Remove(objDelEntry);
objDelEntry.Close();
objDelEntry.Dispose();
return true;
}
catch(Exception err)
{
strErrDescription = err.ToString();
return false;
}
}
/// <summary>
/// 删除一组用户
/// </summary>
/// <param name="ADsPath">LDAP://路径</param>
/// <param name="strUserName">用户名组</param>
/// <returns>bool</returns>
bool IUser.DeleteUser(string ADsPath,string[] strUserName)
{
myEntry = new DirectoryEntry(ADsPath);
DirectoryEntry objDelEntry;
//查询用户名是否存在
try
{
foreach(string strUser in strUserName)
{
string strUserPath= UserPath(myEntry,strUser);
if(strUserPath == "")
{
strErrDescription = "UserName: "+strUserName+" - no fond.";
return false;
}
}
foreach(string strUser in strUserName)
{
string strUserPath= UserPath(myEntry,strUser);
//删除一个用户
try
{
objDelEntry = new DirectoryEntry(strUserPath);
objDelEntry.Parent.Children.Remove(objDelEntry);
objDelEntry.Close();
objDelEntry.Dispose();
return true;
}
catch(Exception err)
{
strErrDescription = err.ToString();
return false;
}
}
return false;
}
catch(Exception err)
{
strErrDescription = err.ToString();
return false;
}
}
/// <summary>
/// 删除一组用户
/// </summary>
/// <param name="strUserName">用户名组</param>
/// <returns>bool</returns>
bool IUser.DeleteUser(string[] strUserName)
{
IUser myUser = (IUser)new ADServices();
return myUser.DeleteUser(CONST_ADSPATH,strUserName);
}
/// <summary>
/// 从组中删除用户
/// </summary>
/// <param name="ADsPath">LDAP://地址</param>
/// <param name="strUserName">用户</param>
/// <param name="strGroup">组名</param>
/// <returns>bool</returns>
bool IUser.DeleteUserFormGroup(string ADsPath,string strUserName, string strGroup)
{
DirectoryEntry objGroupExsited;
myEntry = new DirectoryEntry(ADsPath);
string strGroupPath = GroupPath(myEntry,strGroup);
string strUserPath= UserPath(myEntry,strUserName);
//用户是否存在
if(strUserPath == "")
{
strErrDescription = "UserName: "+strUserName+" - no fond.";
return false;
}
//组名是否存在
if(strGroupPath == "")
{
strErrDescription = "GroupName:"+strGroup+" - no fond.";
return false;
}
//从组中删除用户
try
{
objGroupExsited = new DirectoryEntry(strGroupPath);
objGroupExsited.Invoke("Remove", new Object[] {strUserPath});
objGroupExsited.Close();
objGroupExsited.Dispose();
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);
}
/// <summary>
/// 从组中删除用户名
/// </summary>
/// <param name="ADsPath">LADP://地址</param>
/// <param name="strUserName">用户名</param>
/// <param name="strGroup">组名</param>
/// <returns>bool</returns>
bool IUser.DeleteUserFormGroup(string ADsPath,string[] strUserName, string strGroup)
{
DirectoryEntry objGroupExsited;
myEntry = new DirectoryEntry(ADsPath);
string strGroupPath = GroupPath(myEntry,strGroup);
//组名是否存在
if(strGroupPath == "")
{
strErrDescription = "GroupName:"+strGroup+" - no fond.";
return false;
}
//判断所有用户是否存在
foreach(string strUser in strUserName)
{
string strUserPath= UserPath(myEntry,strUser);
if(strUserPath == "")
{
strErrDescription = "UserName: "+strUserName+" - no fond.";
return false;
}
}
try
{
foreach(string strUser in strUserName)
{
string strUserPath= UserPath(myEntry,strUser);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -