<table style="width:100%;">
<tr>
<td>
請輸入帳號:</td>
<td>
<asp:TextBox ID="txtaccount" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
選擇網域:</td>
<td>
<asp:DropDownList ID="ddldomain" runat="server" Height="28px" Width="155px">
<asp:ListItem Selected="True">coretronic</asp:ListItem>
<asp:ListItem>comple</asp:ListItem>
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btunlock" runat="server" Height="25px" Text="解鎖"
Width="68px" οnclick="btunlock_Click" />
</td>
<td>
</td>
</tr>
</table>
需引用
using System.DirectoryServices;
BIN文件夾需加載本機系統中System.DirectoryServices.dll文件
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void btunlock_Click(object sender, EventArgs e)
{
string name = txtaccount.Text.Trim();
if (string.IsNullOrEmpty(name))
{
Page.RegisterStartupScript("", "<script language = 'javascript' type='text/javascript'>alert('请输入NT账号!')</script>");
return;
}
if (!(ddldomain.SelectedItem != null && ddldomain.SelectedItem.ToString() != ""))
{
Page.RegisterStartupScript("", "<script language = 'javascript' type='text/javascript'>alert('请选择正确的域!')</script>");
return;
}
using (DirectorySearcher s = new DirectorySearcher())
{
//设置查找条件
s.Filter = string.Format("(&(objectClass=user)(sAMAccountname={0}))", name);
using (DirectoryEntry root = new DirectoryEntry("LDAP://OU=xx,OU=xx,DC=" + this.ddldomain.SelectedItem.ToString() + ",DC=com"))
{
//用户名和密码隶属于中有 Domain Users 或 Domain Admins
root.Username = "";
root.Password = "";
s.SearchRoot = root;
try
{
using (DirectoryEntry en = s.FindOne().GetDirectoryEntry())
{
try
{
en.Username = "";
en.Password = "";
bool isLock = false;
//en.Properties["IsAccountLocked"]没有值。
if (en.InvokeGet("IsAccountLocked") is bool)
isLock = (bool)en.InvokeGet("IsAccountLocked");
if (!isLock)
{
Page.RegisterStartupScript("", "<script language = 'javascript' type='text/javascript'>alert('此帐号没有锁定,请再核对!')</script>");
return;
}
en.InvokeSet("IsAccountLocked", false);
en.CommitChanges();
Page.RegisterStartupScript("", "<script language = 'javascript' type='text/javascript'>alert('解锁成功!')</script>");
}
catch
{
Page.RegisterStartupScript("", "<script language = 'javascript' type='text/javascript'>alert('出现未知错误!')</script>");
}
}
}
catch
{
Page.RegisterStartupScript("", "<script language = 'javascript' type='text/javascript'>alert('用户名/域名错误,请核对后重新提交!')</script>");
return;
}
}
}
}