最近把以前工作中用到的一些技术点总结了一下,写了下来
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.DirectoryServices;
![None.gif](/Images/OutliningIndicators/None.gif)
public
partial
class
_Default : System.Web.UI.Page
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
protected void Page_Load(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
//列出所有用户信息
protected void btnListAllUser_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
foreach (DirectoryEntry child in AD.Children)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//列出所有用户信息
switch (child.SchemaClassName)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "User":
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//列出用户信息
DirectoryEntry entryUser = new DirectoryEntry("WinNT://" + Environment.MachineName + "/" + child.Name + ",User");
Response.Write("<br>");
Response.Write(" " + entryUser.Name);
Response.Write("<br>");
Response.Write(" " + " " + entryUser.Properties["Description"].Value);
Response.Write("<br>");
Response.Write("<br>");
Response.Write("<br>");
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("发生错误: " + ex.Message);
Response.Write("<br>");
}
finally
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
break;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//列出一组中的成员
protected void btnGroupUser_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string GroupName = "Users"; //组名
//string GroupName = "Administrators";
DirectoryEntry entryGroup = new DirectoryEntry(@"WinNT://" + Environment.MachineName + "/" + GroupName + ",Group");
Object members = entryGroup.Invoke("Members", null);
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (object member in (IEnumerable)members)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DirectoryEntry x = new DirectoryEntry(member);
Response.Write(x.Name + "<br>"); //用户名称
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(" " + " " + x.Properties["Description"].Value); //用户描述
Response.Write(x.Name + "<br>"); //用户名称
}
catch
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("发生错误: " + ex.Message + "<br>");
}
finally
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//AD所有成员
protected void btnAllChildren_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
foreach (DirectoryEntry child in AD.Children)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//这里会列出所有组和服务的信息
Response.Write(child.Name);
Response.Write("<br>");
Response.Write(child.SchemaClassName);
Response.Write("<br>");
Response.Write("<br>");
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//添加用户
protected void btnAddUser_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
//添加用户,用户名:NewUser
DirectoryEntry NewUser = AD.Children.Add("NewUser", "User");
//设置密码,密码:mypassword
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
NewUser.Invoke("SetPassword", new object[]
{ "mypassword" });
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
NewUser.Invoke("Put", new object[]
{ "Description", "myDescription" });
//提交修改
NewUser.CommitChanges();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//将用户添加到users组
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
object[] objNewUser = new object[]
{ NewUser.Path };
DirectoryEntry groupUser = AD.Children.Find("Users", "group");
groupUser.Invoke("Add", objNewUser);
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("添加用户时发生错误:" + ex.Message + "<br>");
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//修改用户密码
protected void btnChangpwd_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
DirectoryEntry Cuser = AD.Children.Find("NewUser");
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Cuser.Invoke("SetPassword", new object[]
{ "myNewpassword" });
Cuser.CommitChanges();
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("修改密码时发生错误:" + ex.Message + "<br>");
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//把用户从组中移除
protected void btnRemoveUserfromGroup_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
DirectoryEntry entryUser = AD.Children.Find("NewUser", "User");
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
object[] objUser = new object[]
{ entryUser.Path };
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//查找users组
DirectoryEntry grpUsers = AD.Children.Find("Users", "group");
//从User组中移除
grpUsers.Invoke("remove", objUser);
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("将用户从组中移除时发生错误:" + ex.Message + "<br>");
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//删除用户
protected void btnDeleteUser_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer");
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DirectoryEntry ChildUser = AD.Children.Find("NewUser", "User");
if (ChildUser.Name != null && ChildUser.Name != "")
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AD.Children.Remove(ChildUser);
}
}
catch (Exception ex)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("将用户删除时发生错误:" + ex.Message + "<br>");
}
}
}