日期:2014-05-18 浏览次数:20631 次
using System.DirectoryServices;
--先应用
--添加用户
DirectoryEntry entry = new DirectoryEntry(BaseFunctions.DomainSrv,HttpContext.Current.Session["username"].ToString(),HttpContext.Current.Session["password"].ToString(), AuthenticationTypes.Secure);
DirectoryEntry subEntry = entry.Children.Find(ou.Value);
DirectoryEntry deUser = subEntry.Children.Add("cn=" +firstname.Text.ToString()+lastname.Text.ToString(), "user");
deUser.Properties["userPrincipalName"].Add((accountname.Text.ToString()+"@"+BaseFunctions.Domainname));
deUser.Properties["samAccountName"].Add(accountname.Text.ToString());//帐户
deUser.Properties["description"].Value = desc.Text.ToString();
deUser.Properties["sn"].Add(firstname.Text.ToString());
deUser.Properties["givenName"].Add(lastname.Text.ToString());
deUser.Properties["displayName"].Add(firstname.Text.ToString()+lastname.Text.ToString());
deUser.CommitChanges();
deUser.Invoke("ChangePassword",new object[]{"",password.Text.ToString()});
deUser.Properties["userAccountControl"].Value = 0x200;
deUser.CommitChanges();
deUser.Close();
lblShowInfo.Text="添加成功";
------解决方案--------------------
你需要采用管理员身份运行你的iis才可以操作Active Directory
操作代码
http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C
Howto: (Almost) Everything In Active Directory via C#