Asp.Net 修改域账号密码 代码操作域AD

DirectoryEntry AD = new DirectoryEntry();
AD.Path = string.Format("LDAP://{0}", strPath);
AD.Username = @"domain\username";
AD.Password = "PassWord";
AD.AuthenticationType = AuthenticationTypes.Secure;
string Msg = string.Empty;
try
{
DirectorySearcher searcher = new DirectorySearcher(AD);
searcher.Filter = String.Format("(&(objectClass=user)(samAccountName={0}))", username);
System.DirectoryServices.SearchResult result = searcher.FindOne();
if (result != null)
{
DirectoryEntry userEntry = result.GetDirectoryEntry();
if (userEntry != null)
{
try
{
userEntry.Invoke("ChangePassword", new Object[] { OldPassWord, NewPassWord });
userEntry.CommitChanges();
userEntry.Close();
Msg = "修改密码成功!";
}
catch (Exception ex)
{
Msg = "操作失败,未知账号或密码错误!";
}
}
}
else
{
Msg = "操作失败,未知账号或密码错误!";
}
AD.Close();
}
catch (Exception ex)
{
Msg = "操作失败,错误原因:" + ex.ToString();
}

posted @ 2014-07-01 17:17  blue-K2  阅读(871)  评论(0)    收藏  举报