public ActionResult CreateUser(AUser AdUser) { if (string.IsNullOrEmpty(AdUser.sAMAccountName)) { throw new ArgumentException("账户名不能为空"); } try { Core.ADManager.Create(AdUser); } catch (Exception ex) { throw new ArgumentException(ex.Message); } return RedirectToAction("User"); }
/// <summary> /// 添加用户 /// </summary> /// <param name="AdUser"></param> public void Create(AUser AdUser) { DirectoryEntry UserGroup = Get("(&(OU="+AdUser.Orginzation+"))"); if (UserGroup == null) { throw new ArgumentException("未找到创建用户的组织单元"); } DirectoryEntry user = UserGroup.Children.Add("CN=" + AdUser.Initial, "user"); UserGroup.Close(); user.Properties["sAMAccountName"].Value = AdUser.sAMAccountName; user.Properties["sn"].Value = AdUser.Sn; user.Properties["givenName"].Value = AdUser.GivenName; if (AdUser.Day != 0 || AdUser.Month != 0 || AdUser.Year != 0) { if (user.Properties.Contains("accountExpires")) { user.Properties["accountExpires"].Value = DateTime.Now.ToString(); } } user.CommitChanges(); user.Close(); }