//private static DirectoryEntry GetUserDetailsPC(string accountName) //{ // bizSetting bizS = new bizSetting(); // string LDAP = bizS.GetSetting("ActiveDirectory.LDAP"); //what to do with SMI domain ?? TODO // PrincipalContext adPrincipalContext = // new PrincipalContext( // ContextType.Domain, // LDAP.Replace("LDAP://", string.Empty)); // UserPrincipal user = new UserPrincipal(adPrincipalContext); // user.Enabled = true; // user.SamAccountName = accountName; // //Test that the wildcard search actually works // PrincipalSearcher pS = new PrincipalSearcher(user); // Principal results = pS.FindOne(); // if (results == null) return null; // DirectoryEntry objEntry = (DirectoryEntry)results.GetUnderlyingObject(); // return objEntry; //} //public static DirectoryEntry GetUserDetails(string LDAP, string accountName) //{ // return GetUserDetailsPC(LDAP, accountName); // /* // DirectoryEntry objEntry = new DirectoryEntry(LDAP); // DirectorySearcher searcher = new DirectorySearcher(objEntry); // searcher.Filter = "(&(objectClass=user)(objectCategory=person)(SAMAccountName=" + accountName + "))"; // searcher.PropertiesToLoad.Add("cn"); // searcher.PropertiesToLoad.Add("Department"); // SearchResult result = null; // result = searcher.FindOne(); // if (result == null) return null; // return result.GetDirectoryEntry(); // */ //} public static string GetUserFullName(string accountName) { string returnValue = string.Empty; try { bizSetting bizS = new bizSetting(); accountName = accountName.Replace(bizS.GetSetting("Security.DomainName"), ""); accountName = accountName.Replace(bizS.GetSetting("Security.DomainNameSmi"), ""); //map to the SMI user account name, etc //string SmiUserName = bizUser.GetAccountExecutiveIdBySmiUserName(accountName); returnValue = bizUser.GetFullNameBySmiUserName(accountName); // AD LDS or AD context //DirectoryEntry results = GetUserDetailsPC(SmiUserName); //if (results == null) return null; //return results.Properties["cn"][0].ToString(); //end of code addition /* * //DirectoryEntry objEntry = new DirectoryEntry(LDAP); * DirectorySearcher searcher = new DirectorySearcher(objEntry); * * searcher.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + accountName + "))"; * searcher.PropertiesToLoad.Add("cn"); * * SearchResult result = null; * result = searcher.FindOne(); * * if (result == null) * { * return null; * } * * return result.Properties["cn"][0].ToString(); * */ } catch (Exception) { //return String.Empty; } return(returnValue); }
public static Dictionary <string, enGageUser> GetUsersAccountExecutives(List <string> usernames) { bizSetting biz = new bizSetting(); // List of sample results. List <string> loadedUsers = new List <string>(); Dictionary <string, enGageUser> enGageUsersResults = new Dictionary <string, enGageUser>(); if (usernames == null || usernames.Count == 0) { return(enGageUsersResults); } string LDAP = biz.GetSetting("ActiveDirectory.LDAP").Replace("LDAP://", string.Empty); using (DirectorySearcher searcher = new DirectorySearcher(LDAP)) { StringBuilder filterStringBuilder = new StringBuilder(); // Just create a single LDAP query for all user SIDs filterStringBuilder.Append("(&(objectClass=user)(|"); foreach (string userSid in usernames) { filterStringBuilder.AppendFormat("({0}={1})", "SamAccountName", userSid); } filterStringBuilder.Append("))"); searcher.PageSize = 1000; // Very important to have it here. Otherwise you'll get only 1000 at all. Please refere to DirectorySearcher documentation searcher.Filter = filterStringBuilder.ToString(); // We do not want to go beyond GC searcher.ReferralChasing = ReferralChasingOption.None; searcher.PropertiesToLoad.AddRange( new[] { "DistinguishedName", "cn", "Department", "SamAccountName" }); SearchResultCollection results = searcher.FindAll(); // 1- get all departement branches Dictionary <string, string> departementRegions = new Dictionary <string, string>(); List <string> tempDepartments = new List <string>(); foreach (SearchResult searchResult in results) { var deptKey = searchResult.Properties["Department"][0].ToString(); if (!departementRegions.ContainsKey(deptKey)) { departementRegions.Add(deptKey, GetRegionForBranch(deptKey)); } // add the result in one go var userKey = searchResult.Properties["SamAccountName"][0].ToString(); if (!enGageUsersResults.ContainsKey(userKey)) { enGageUsersResults.Add(userKey, new enGageUser { DisplayName = searchResult.Properties["cn"][0].ToString(), UserName = userKey, Branch = deptKey, Region = departementRegions[deptKey], Role = 0 }); } } ; //foreach (SearchResult searchResult in results) //{ // string distinguishedName = searchResult.Properties["DistinguishedName"][0].ToString(); // loadedUsers.Add(distinguishedName); //} } return(enGageUsersResults); }
public static enGageUser GetCurrentUser(string currentLogonWithoutAdPrefix) { //get the SMI user name string SmiUserName = GetAccountExecutiveIdBySmiUserName(currentLogonWithoutAdPrefix); System.Security.Principal.WindowsImpersonationContext impersonationContext; impersonationContext = ((System.Security.Principal.WindowsIdentity)WindowsIdentity.GetCurrent()).Impersonate(); //get the adctive directory connection bizSetting biz = new bizSetting(); string LDAP = biz.GetSetting("ActiveDirectory.LDAP").Replace("LDAP://", string.Empty); PrincipalContext adPrincipalContext = new PrincipalContext(ContextType.Domain, LDAP, null, null); UserPrincipal userP = new UserPrincipal(adPrincipalContext); userP.Enabled = true; userP.SamAccountName = currentLogonWithoutAdPrefix; //updated to use the new SMI user name sourced from SQL //Test that the wildcard search actually works PrincipalSearcher pS = new PrincipalSearcher(userP); Principal results = pS.FindOne(); if (results == null) { return(null); } DirectoryEntry de = (DirectoryEntry)results.GetUnderlyingObject(); /* * bizSetting biz = new bizSetting(); * System.DirectoryServices.DirectoryEntry de = * // bizActiveDirectory.GetUserDetails(biz.GetSetting("ActiveDirectory.LDAP"), * HttpContext.Current.User.Identity.Name.Replace("OAMPSINS\\", "")); */ if (de == null) { return(null); } enGageUser user = new enGageUser(); user.SMIUserName = currentLogonWithoutAdPrefix; //yes, this is correct, save the current domain user logon user.UserName = SmiUserName; //for mapping over to AD, need to use the 'translated from SQL' logon user.DisplayName = de.Properties["cn"].Value.ToString(); user.Branch = de.Properties["Department"].Value.ToString(); user.Region = GetRegionForBranch(user.Branch); user.Role = 0; return(user); //// FOR TESTING PURPOSES //enGageUser user = new enGageUser //{ // SMIUserName = username, // UserName = GetAccountExecutiveIdBySmiUserName(username), // DisplayName = "Testing Account", // Branch = "Testing Branch", // Region = "Testing Region", // Role = 0 //}; // return user; }
public static String ListAccountExecutivesByBranch(string branch) { var bizS = new bizSetting(); //string LDAP = bizS.GetSetting("ActiveDirectory.LDAP").Replace("LDAP://", string.Empty); string groupName = bizS.GetSetting("Security.ADExecutiveGroupDisplayName"); //////////////////////////////////////////////// //PrincipalContext adPrincipalContext = new PrincipalContext(ContextType.Domain, LDAP); PrincipalContext adPrincipalContext = new PrincipalContext(ContextType.Domain); GroupPrincipal group = new GroupPrincipal(adPrincipalContext); group.SamAccountName = "*" + groupName + "*"; //Test that the wildcard search actually works PrincipalSearcher pS = new PrincipalSearcher(group); Principal results = pS.FindOne(); if (results == null) { return(null); } DirectoryEntry objEntry = (DirectoryEntry)results.GetUnderlyingObject(); //return objEntry; //////////////////////////////////////////////// /* * // DirectoryEntry objEntry = new DirectoryEntry(LDAP); * DirectorySearcher searcher = new DirectorySearcher(objEntry); * * searcher.PropertiesToLoad.Add("member"); * searcher.Filter = "(SAMAccountName=" + groupName + ")"; * * SearchResult result = null; * result = searcher.FindOne(); * * if (result == null) return null; * * DirectoryEntry group = result.GetDirectoryEntry(); */ object members = objEntry.Invoke("Members", null); String data = ""; foreach (object member in (IEnumerable)members) { DirectoryEntry user = new DirectoryEntry(member); if (user.Properties["Department"].Value != null) { if (user.Properties["Department"].Value.ToString() == branch) { data += "|" + user.Properties["SAMAccountName"].Value.ToString(); } } } if (data != "") { data = data.Remove(0, 1); } return(data); }
public static NameValueCollection FindAccountExecutive(string name) { var bizS = new bizSetting(); string LDAP = bizS.GetSetting("ActiveDirectory.LDAP"); string groupName = bizS.GetSetting("Security.ADExecutiveGroupDisplayName"); PrincipalContext adPrincipalContext = new PrincipalContext( ContextType.Domain, LDAP.Replace("LDAP://", string.Empty)); GroupPrincipal groupP = new GroupPrincipal(adPrincipalContext); groupP.Name = groupName; //Test that the wildcard search actually works PrincipalSearcher pS = new PrincipalSearcher(groupP); Principal results = pS.FindOne(); if (results == null) { return(null); } //DirectoryEntry objEntry = (DirectoryEntry)results.GetUnderlyingObject(); ///////////////////////////////////////////// NameValueCollection nvc = new NameValueCollection(); DirectoryEntry group = (DirectoryEntry)results.GetUnderlyingObject(); object members = group.Invoke("Members", null); foreach (object member in (IEnumerable)members) { DirectoryEntry user = new DirectoryEntry(member); if (user.Properties["Department"].Value != null) { if (user.Properties["DisplayName"].Value.ToString().ToLowerInvariant().Contains(name.ToLowerInvariant()) == true) { nvc.Add(user.Properties["DisplayName"].Value.ToString() + ", " + user.Properties["Department"].Value.ToString(), user.Properties["SAMAccountName"].Value.ToString()); } } } return(nvc); //NameValueCollection nvc = new NameValueCollection(); //bizSetting biz = new bizSetting(); ////Removing the hard coding of AD I will need to retrieve this value from the database. //string adDetails = biz.GetSetting("Security.ADExecutiveDetails"); //PrincipalContext adPrincipalContext = new PrincipalContext(ContextType.Domain, LDAP.Replace("LDAP://", string.Empty), adDetails); //UserPrincipalEx testuser = new UserPrincipalEx(adPrincipalContext); //testuser.DisplayName = "*" + name + "*"; //PrincipalSearcher pS = new PrincipalSearcher(testuser); //DirectorySearcher searcher = (DirectorySearcher)pS.GetUnderlyingSearcher(); //searcher.PropertiesToLoad.Add("DisplayName"); //searcher.PropertiesToLoad.Add("SAMAccountName"); //var results = pS.FindAll(); //foreach (Principal user in results) // nvc.Add(user.DisplayName, user.SamAccountName); //return nvc; }
//public static string GetUserAccountName(string LDAP, string fullName) //{ // PrincipalContext adPrincipalContext = // new PrincipalContext( // ContextType.Domain, // LDAP.Replace("LDAP://", string.Empty)); // UserPrincipal user = new UserPrincipal(adPrincipalContext); // user.Enabled = true; // user.Name = "*" + fullName + "*"; // //Test that the wildcard search actually works // PrincipalSearcher pS = new PrincipalSearcher(user); // Principal results = pS.FindOne(); // if (results == null) return null; // DirectoryEntry objEntry = (DirectoryEntry)results.GetUnderlyingObject(); // return objEntry.Properties["sAMAccountName"][0].ToString(); // /* // DirectoryEntry objEntry = new DirectoryEntry(LDAP); // DirectorySearcher searcher = new DirectorySearcher(objEntry); // searcher.Filter = "(&(objectClass=user)(objectCategory=person)(cn=" + fullName + "))"; // searcher.PropertiesToLoad.Add("SAMAccountName"); // SearchResult result = null; // result = searcher.FindOne(); // if (result == null) // { // return null; // } // return result.Properties["sAMAccountName"][0].ToString(); // * */ //} public static NameValueCollection ListAccountExecutivesByBranchForDropDown(string branch) { var bizS = new bizSetting(); //string LDAP = bizS.GetSetting("ActiveDirectory.LDAP"); string groupName = bizS.GetSetting("Security.ADExecutiveGroupDisplayName"); //need to have SMI one ?? if (String.Equals(branch, "Melbourne Commercial and Industry", StringComparison.OrdinalIgnoreCase)) { branch = @"Melbourne Commercial &Industry"; } else if (String.Equals(branch, "Clayton", StringComparison.OrdinalIgnoreCase)) { branch = @"Instrat"; } NameValueCollection nvc = new NameValueCollection(); /* //Commented to remove hardcoding * PrincipalContext adPrincipalContext = * new PrincipalContext( * ContextType.Domain, * // LDAP.Replace("LDAP://", string.Empty), "OU=People,OU=OAMPS User Objects,DC=oamps,DC=com,DC=au"); */ PrincipalContext adPrincipalContext = new PrincipalContext(ContextType.Domain, "SMI"); UserPrincipalEx testuser = new UserPrincipalEx(adPrincipalContext); //This is a fix for the hard coding of the AD //Searching through AD for the Distinguish Name GroupPrincipal qbeGroup = new GroupPrincipal(adPrincipalContext); PrincipalSearcher srch = new PrincipalSearcher(qbeGroup); var searchPrinciple = new UserPrincipal(adPrincipalContext); string groupDn = string.Empty; foreach (var found in srch.FindAll()) { GroupPrincipal foundGroup = found as GroupPrincipal; if (foundGroup != null) { if (foundGroup.Name == groupName) { groupDn = foundGroup.DistinguishedName; } } } groupDn = groupDn != string.Empty ? groupDn : groupName; testuser.AdvancedSearchFilter.GroupName(groupDn, MatchType.Equals); //Area to change testuser.AdvancedSearchFilter.DepartmentName(branch, MatchType.Equals); PrincipalSearcher pS = new PrincipalSearcher(testuser); DirectorySearcher searcher = (DirectorySearcher)pS.GetUnderlyingSearcher(); searcher.PropertiesToLoad.Add("DisplayName"); searcher.PropertiesToLoad.Add("SAMAccountName"); var results = pS.FindAll(); foreach (Principal user in results) { nvc.Add(user.DisplayName, user.SamAccountName); } return(nvc); }