Ejemplo n.º 1
0
        public ActionResult PCreateUser(CreateModel cModel)
        {
            ADInfo adinfo  = new ADInfo();
            var    OUgroup = adinfo.GetOU();

            cModel.OrgUnit = adinfo.GetSelectListItems(OUgroup);
            string test = cModel.SelectedOU;

            if (ModelState.IsValid)
            {
                try
                {
                    using (UserPrincipal uP = adinfo.GetUser(cModel.SamAccountName, false))
                    {
                        if (uP == null)
                        {
                            PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
                                                                        "newgate-software.local",
                                                                        "ou=" + cModel.SelectedOU + ",dc=newgate-software,dc=local",
                                                                        "NEWGATE-SOFTWAR\\Administrator",
                                                                        "P@ssw0rd123");
                            UserPrincipal newUser = new UserPrincipal(ctx, cModel.SamAccountName, cModel.Password, true)
                            {
                                GivenName            = cModel.DisplayName,
                                PasswordNeverExpires = true,
                                Enabled = true
                            };
                            newUser.Save();

                            ViewBag.Message = "Success";
                            ModelState.Clear();
                            return(PartialView("_CreateUser", cModel));
                        }
                        else
                        {
                            ViewBag.Message = "Username existed";
                            ModelState.Clear();
                            return(PartialView("_CreateUser", cModel));
                        }
                    }
                }
                catch (Exception e)
                {
                    ViewBag.Message = e.Message + e.StackTrace + e.InnerException;
                    ModelState.Clear();
                    return(PartialView("_CreateUser", cModel));
                }
            }
            return(PartialView("_CreateUser", cModel));
        }