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 })); }
public static string GetJobTitle(Staff staff) { string foundYou = ""; using (var pc = new PrincipalContext(ContextType.Domain, "MILSTED-LANGDON")) { UserPrincipalEx user = UserPrincipalEx.FindByIdentity(pc, IdentityType.SamAccountName, "MILSTED-LANGDON\\" + staff.username); foundYou = user.Title; } return(foundYou); }
//if you want to get Groups of Specific OU you have to add OU Name in Context public static List <User> GetallAdUsers() { List <User> AdUsers = new List <User>(); var ctx = new PrincipalContext(ContextType.Domain, "ad.balkangraph.com", "OU=TestOU,DC=ad,DC=balkangraph,DC=com"); UserPrincipal userPrin = new UserPrincipal(ctx); userPrin.Name = "*"; var searcher = new PrincipalSearcher(); searcher.QueryFilter = userPrin; var results = searcher.FindAll(); foreach (Principal p in results) { UserPrincipalEx extp = UserPrincipalEx.FindByIdentity(ctx, IdentityType.DistinguishedName, p.DistinguishedName); var managerCN = extp.Manager; var mgr = ""; string[] list = managerCN.Split(','); if (managerCN.Length > 0) { mgr = list[0].Substring(3); } string picture = ""; if (extp.ThumbnailPhoto != null) { picture = Convert.ToBase64String(extp.ThumbnailPhoto); } AdUsers.Add(new User { Id = extp.DistinguishedName, Pid = extp.Manager, DisplayName = extp.DisplayName, SamAccountName = extp.SamAccountName, Manager = mgr, Image = "data:image/jpeg;base64," + picture, JobTitle = extp.Title, Company = extp.Company, Phone = extp.TelephoneNumber, Disabled = (extp.Enabled == false) ? "disabled" : "enabled" }); } return(AdUsers); }
//Check User input in active directory domain public ActionResult checkUserAD(string userName) { string domain = userName.Substring(0, userName.IndexOf("\\")); string userId = userName.Substring(userName.IndexOf("\\") + 1, userName.Length - (userName.IndexOf("\\") + 1)); string UserDomain = "Domain : " + domain + " UserId : " + userId; string FullDomainName = "Yes".Equals(ConfigurationManager.AppSettings["IsProduction"].ToString()) ? ConfigurationManager.AppSettings["FullDomainName"].ToString() : ConfigurationManager.AppSettings["FullDomainName_dev"].ToString(); string ContainerDomainName = "Yes".Equals(ConfigurationManager.AppSettings["IsProduction"].ToString()) ? ConfigurationManager.AppSettings["ContainerDomainName"].ToString() : ConfigurationManager.AppSettings["ContainerDomainName_dev"].ToString(); try { using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, FullDomainName, ContainerDomainName, ContextOptions.Negotiate)) { string message = ""; using (UserPrincipalEx foundUser = UserPrincipalEx.FindByIdentity(domainContext, IdentityType.SamAccountName, userId)) { if (foundUser == null) { message = Resources.NotifResource.DomainNameNotFoundAD; return(Json(new { isExist = 0, message = message }, JsonRequestBehavior.AllowGet)); } else { var adId = foundUser.Guid; string objSid = foundUser.Sid.ToString(); if (foundUser.GivenName == null) { foundUser.GivenName = ""; } if (foundUser.Surname == null) { foundUser.Surname = ""; } return(Json(new { isExist = 1, fname = foundUser.GivenName, lname = foundUser.Surname, objSid = objSid, message = message, adId = adId, email = foundUser.EmailAddress, title = foundUser.Title, phone = foundUser.homePhone }, JsonRequestBehavior.AllowGet)); } } } } catch (Exception ex) { Logger log = NLog.LogManager.GetCurrentClassLogger(); string message = "Incorrect Domain Server"; log.Error(ex.StackTrace + "====Inner==== :" + ex.InnerException.ToString() + " ====Message :" + ex.Message); return(Json(new { isExist = 0, message = message }, JsonRequestBehavior.AllowGet)); } }
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()); }
private UserPrincipalEx GetUser(string samAccountName) { var userPrincipal = UserPrincipalEx.FindByIdentity(_ctx, IdentityType.SamAccountName, samAccountName); return(userPrincipal); }