private void Init() { if (!IsInit) { List <string> adDomains = BIASettingsReader.BIANetSection?.Authentication?.Parameters?.ADDomains; GroupPrincipal group = null; if (adDomains != null) { foreach (string domain in adDomains) { try { PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain); group = GroupPrincipal.FindByIdentity(ctx, GroupName); if (group != null) { Domain = domain; Group = group; IsValid = true; TraceManager.Debug("Group " + GroupName + " not in domain " + domain); break; } } catch (Exception e) { TraceManager.Warn("Could not join Domain :" + domain, e); } TraceManager.Debug("Group " + GroupName + " not found in domain " + domain); } } } IsInit = true; }
/// <summary> /// <summary> /// Gets the groups of the user. /// </summary> /// <param name="userName">Name of the user.</param> /// <returns>the list of the group of the user</returns> public static List <string> GetGroups(string userName) { List <string> adDomains = BIASettingsReader.BIANetSection?.Authentication?.Parameters?.ADDomains; TraceManager.Debug("ADHelper", "GetGroups", "Begin for : " + userName); if (adDomains != null) { foreach (string domain in adDomains) { try { WindowsIdentity wi = new WindowsIdentity(userName + "@" + domain); if (wi != null) { TraceManager.Info("ADHelper", "GetGroups", "User : "******" find in Domain : " + domain); return(GetGroups(wi)); } } catch (Exception) { TraceManager.Info("ADHelper", "GetGroups", "Could not find user : "******" in Domain : " + domain); } } } return(new List <string>()); }
public static void Dispose(Guid guid) { ProjectDBContainer dbCont = null; if (guid != default(Guid) && _dbs.TryGetValue(guid, out dbCont)) { lock (SyncRootDbs) { if (guid != default(Guid) && _dbs.TryGetValue(guid, out dbCont)) { if (dbCont != null && dbCont.db != null) { lock (dbCont.SyncRootDb) { if (dbCont != null && dbCont.db != null) { dbCont.db.Dispose(); } } } _dbs.Remove(guid); TraceManager.Debug("Model.DAL.ProjectDBContainer", "Dispose", "Nb Context: " + _dbs.Count); } } } }
public void Dispose() { if (this._db != null) { this._db.Dispose(); TraceManager.Debug("TDBContainer<ProjectDBContext>", "Dispose", "dispose context " + this.contextKey); } this._db = null; }
public static void Init(Guid guid, ProjectDBContainer projectDBContainer = null) { TraceManager.Debug("Model.DAL.ProjectDBContainer", "Init", "Nb Context: " + _dbs.Count); if (guid != default(Guid) && !_dbs.ContainsKey(guid)) { lock (SyncRootDbs) { if (guid != default(Guid) && !_dbs.ContainsKey(guid)) { _dbs.Add(guid, projectDBContainer ?? new ProjectDBContainer()); } } } }
/// <summary> /// Gets the user in group. /// </summary> /// <param name="groupName">Name of the group.</param> /// <returns>List of user in group</returns> public List <UserPrincipal> GetAllUsersInGroup() { Init(); List <UserPrincipal> listUsers = new List <UserPrincipal>(); List <GroupPrincipal> listTreatedGroups = new List <GroupPrincipal>(); // if found.... if (IsValid) { TraceManager.Debug("Group " + GroupName + " is valid"); GetAllUsersFromGroupRecursivly(Group, listUsers, listTreatedGroups); } else { TraceManager.Debug("Group " + GroupName + " is not valid"); } return(listUsers); }
private static void GetAllUsersFromGroupRecursivly(GroupPrincipal group, List <UserPrincipal> listUsers, List <GroupPrincipal> listTreatedGroups) { listTreatedGroups.Add(group); TraceManager.Debug("Treat group " + group.Name); try { // iterate over members foreach (Principal p in group.GetMembers()) { Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName); TraceManager.Debug("Treat member " + p.Name); // do whatever you need to do to those members if (p is UserPrincipal) { UserPrincipal theUser = p as UserPrincipal; if (!listUsers.Select(u => u.DistinguishedName).Contains(theUser.DistinguishedName)) { listUsers.Add(theUser); } } else if (p is GroupPrincipal) { GroupPrincipal theGroup = p as GroupPrincipal; if (!listTreatedGroups.Select(g => g.DistinguishedName).Contains(theGroup.DistinguishedName)) { GetAllUsersFromGroupRecursivly(theGroup, listUsers, listTreatedGroups); } } } TraceManager.Debug("Group " + group.Name + " treated."); } catch (Exception e) { TraceManager.Warn("GetAllUsersFromGroupRecursivly crash", e); } }
public static List <string> GetGroups(WindowsIdentity wi) { List <string> result = new List <string>(); if (wi != null) { TraceManager.Debug("ADHelper", "GetGroups", "Nb group in AD : " + wi.Groups.Count); foreach (IdentityReference group in wi.Groups) { try { string groupName = ""; string groupValue = group.Value; if (!CacheGroupName.TryGetValue(groupValue, out groupName)) { TraceManager.Debug("Try resolve name : " + groupValue); groupName = group.Translate(typeof(NTAccount)).ToString(); CacheGroupName.Add(groupValue, groupName); TraceManager.Debug("Name resolve : " + groupName); } result.Add(groupName); } catch (Exception e) { TraceManager.Debug("Error"); TraceManager.Warn("Error when treat " + group.Value, e); } } result.Sort(); TraceManager.Debug("ADHelper", "GetGroups", "End"); } return(result); }