public JsonResult AddAccount(string pid, string name, string displayName) { var ctx = new PrincipalContext(ContextType.Domain, "ad.balkangraph.com", "OU=TestOU,DC=ad,DC=balkangraph,DC=com"); var up = new UserPrincipal(ctx, name, "tempP@ssword", true); up.DisplayName = displayName; up.Name = displayName; up.Save(); UserPrincipal userPrin = new UserPrincipal(ctx); userPrin.Name = "*"; var searcher = new PrincipalSearcher(); searcher.QueryFilter = userPrin; var results = searcher.FindAll(); UserPrincipalEx extpsdf = null; foreach (Principal p in results) { UserPrincipalEx extp = UserPrincipalEx.FindByIdentity(ctx, IdentityType.DistinguishedName, p.DistinguishedName); if (extp.SamAccountName == name) { extp.Manager = pid; extp.Save(); extpsdf = extp; } } return(Json(new { id = extpsdf.DistinguishedName, displayName = extpsdf.DisplayName })); }
/// <summary> /// Sets the primary group for the specified User principal to "Domain Guests". /// </summary> /// <param name="userPrincipal">The principal of the account in Active Directory that you'd like to set the primary group for.</param> public static bool ToDomainGuests(this UserPrincipalEx userPrincipal) { bool wasSuccessful = false; if (userPrincipal != null) { userPrincipal.PrimaryGroupId = (int)PrimaryGroupId.DomainGuests; userPrincipal.Save(); wasSuccessful = true; } return(wasSuccessful); }
public EmptyResult UpdateUser(User user) { var ctx = new PrincipalContext(ContextType.Domain, "ad.balkangraph.com", "OU=TestOU,DC=ad,DC=balkangraph,DC=com"); UserPrincipalEx userPrin = UserPrincipalEx.FindByIdentity(ctx, IdentityType.DistinguishedName, user.Id); if (user.SamAccountName != null) { userPrin.SamAccountName = user.SamAccountName; } // userPrin.DisplayName = user.DisplayName; userPrin.Title = user.JobTitle; userPrin.TelephoneNumber = user.Phone; userPrin.Company = user.Company; userPrin.Save(); return(new EmptyResult()); }
public static bool Update(this UserPrincipalEx userPrincipal, UpdateUserRequest request) { var wasSuccessful = false; if (request == null) { return(wasSuccessful); } if (userPrincipal == null) { return(wasSuccessful); } //userPrincipal.AltRecipient = null; userPrincipal.Description = request.Description; userPrincipal.Enabled = request.Enabled; userPrincipal.IpPhone = request.IpPhone; //userPrincipal.MsExchHideFromAddressLists = request.MsExchHideFromAddressLists; userPrincipal.Save(); wasSuccessful = true; return(wasSuccessful); }