-
c#修改工作组win2003密码和账号维护
using<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>;
using<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>.Collections.Generic;
using<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>.Text;
using<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>.DirectoryServices;
namespaceChangePwdWebpart
{
classOperateDirectory
{
//修改密码 publicstaticboolChangePwd(stringUserName,stringoldPwd,stringnewPwd)
{
try
{
DirectoryEntryMachineDirectoryEntry;
MachineDirectoryEntry=newDirectoryEntry("WinNT://"+<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>.Environment.MachineName);
DirectoryEntryCurrentDirectoryEntry=MachineDirectoryEntry.Children.Find(UserName);
CurrentDirectoryEntry.Invoke("ChangePassword",newObject[]{oldPwd,newPwd});
CurrentDirectoryEntry.CommitChanges();
CurrentDirectoryEntry.Close();
returntrue;
}
catch(Exceptionexp)
{
if(exp.InnerException.Message.Replace("'","").IndexOf("网络密码不正确")!=-1)
thrownewException("密码修改失败,输入的原始密码不正确");
else
thrownewException(exp.InnerException.Message);
}
finally
{
}
}
//新增组: privatevoidAddGroup()
{
stringpath=String.Format("WinNT://{0}",<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>.Environment.MachineName);
DirectoryEntryentryPC=newDirectoryEntry(path);
DirectoryEntrynewEntry=entryPC.Children.Add("NewGroup","Group");
//newEntry.Properties["groupType"][0]="4";
newEntry.Properties["Description"].Add("test");
newEntry.CommitChanges();
}
//删除组: privatevoidDelGroup()
{
stringuserGroup="NewGroup";
stringpath1=String.Format("WinNT://{0}",<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>.Environment.MachineName);
DirectoryEntryparent=newDirectoryEntry(path1);
object[]paras=newobject[2];
paras[0]="group";
paras[1]=userGroup;
parent.Invoke("Delete",paras);
}
//查找组: privatevoidFindGroup()
{
stringuserGroup="NewGroup";
stringpath1=String.Format("WinNT://{0}",<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>.Environment.MachineName);
DirectoryEntryparent=newDirectoryEntry(path1);
DirectoryEntrygroup=parent.Children.Find(userGroup,"group");
if(group!=null)
MessageBox.Show("Groupfind.");
else
MessageBox.Show("Groupnotfound.");
}
//新增用户: privatevoidAddUser()
{
stringpath=String.Format("WinNT://{0}",<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>.Environment.MachineName);
DirectoryEntryentryPC=newDirectoryEntry(path);
DirectoryEntryobUser=entryPC.Children.Add("NewUser","User");
obUser.Properties["Description"].Add("TestUserfrom.NET");
obUser.Properties["FullName"].Add("NewUser");
objectobRet=obUser.Invoke("SetPassword","123");
obUser.CommitChanges();
}
//删除用户: privatevoidDeluser()
{
stringuserName="NewUser";
stringpath1=String.Format("WinNT://{0}",<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>.Environment.MachineName);
DirectoryEntryparent=newDirectoryEntry(path1);
object[]paras=newobject[2];
paras[0]="user";
paras[1]=userName;
parent.Invoke("Delete",paras);
}
//查找用户: privatevoidfindUser()
{
stringuserName="NewUser";
stringpath1=String.Format("WinNT://{0}",<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>.Environment.MachineName);
DirectoryEntryparent=newDirectoryEntry(path1);
DirectoryEntryuser=parent.Children.Find(userName,"user");
if(user!=null)
MessageBox.Show("Userfind.");
else
MessageBox.Show("Usernotfound.");
}
}
}