Beispiel #1
0
        public static List <DomainUsers> GetDomainUsers(string group = null)
        {
            List <DomainUsers> listUsers = new List <DomainUsers>();

            try
            {
                //PrincipalContext AD = new PrincipalContext(ContextType.Domain, "LDAP://172.16.10.2");
                PrincipalContext AD = new PrincipalContext(ContextType.Domain, ConfigReader.GetLdapServer());

                UserPrincipal     u      = new UserPrincipal(AD);
                PrincipalSearcher search = new PrincipalSearcher(u);
                PrincipalSearchResult <Principal> searchResult = null;

                searchResult = search.FindAll();

                var usersList = searchResult.Where(s => string.IsNullOrEmpty(group) ||
                                                   s.GetGroups().Any(t => t.Name == group)).ToList();

                foreach (var result in usersList)
                {
                    listUsers.Add(new DomainUsers
                    {
                        UserName = result.SamAccountName,
                        FullName = result.Name
                    });
                }

                return(listUsers);
            }
            catch (Exception ex)
            {
            }

            return(listUsers);
        }
Beispiel #2
0
        public static bool CheckGroupMembership(string userID, string groupName, string Domain)
        {
//#if TRACE
//            long startTicks = VNC.AppLog.Trace5("Start", LOG_APPNAME);
//#endif

            bool isMember = false;

            PrincipalSearchResult <Principal> groups = GetAuthorizationGroupsMembership(userID, Domain);

//#if TRACE
//            VNC.AppLog.Trace5("After GetAuthorizationGroupsMembership", LOG_APPNAME, startTicks);
//#endif


//#if TRACE
//            VNC.AppLog.Trace5(string.Format("After GetAuthorizationGroupsMembership {0}", groups.Count()), LOG_APPNAME, startTicks);
//#endif
            Principal foo = groups.First(g => g.Name == groupName);

//#if TRACE
//            VNC.AppLog.Trace5("After First", LOG_APPNAME, startTicks);
//#endif

            if (foo != null)
            {
                isMember = true;
            }
            int count = groups.Where(g => g.Name == groupName).Count();

//#if TRACE
//            VNC.AppLog.Trace5(string.Format("After Where {0}", count), LOG_APPNAME, startTicks);
//#endif

            using (PrincipalContext ADDomain = new PrincipalContext(ContextType.Domain, Domain))
            {
//#if TRACE
//                VNC.AppLog.Trace5("After new Principal", LOG_APPNAME, startTicks);
//#endif

                using (UserPrincipal user = UserPrincipal.FindByIdentity(ADDomain, userID))
                {
//#if TRACE
//                    VNC.AppLog.Trace5("After FindByIdentity", LOG_APPNAME, startTicks);
//#endif

                    if (count > 0)
                    {
                        isMember = true;
                    }
                }
            }

//#if TRACE
//            VNC.AppLog.Trace5("End", LOG_APPNAME, startTicks);
//#endif

            return(isMember);
        }
        public static IEnumerable <string> GetMembership(UserPrincipal user)
        {
            GroupMembershipResult.Clear();
            GroupNamesAndDescriptions.Clear();

            if (user == null)
            {
                log.Error("UserPrincipal object is NULL!");
                return(new List <string>());
            }

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            try
            {
                using (PrincipalSearchResult <Principal> groups = user.GetGroups(new PrincipalContext(ContextType.Domain, Environment.UserDomainName)))
                {
                    GroupMembershipResult.AddRange(
                        groups.Where(x => x != null && x.SamAccountName != string.Empty)
                        .Select(x => x.SamAccountName.ToUpper())
                        .OrderBy(x => x)
                        .Distinct());
                }

                using (PrincipalSearchResult <Principal> groups = user.GetGroups(new PrincipalContext(ContextType.Domain, Environment.UserDomainName)))
                {
                    GroupNamesAndDescriptions.AddRange(
                        groups.Where(x => x != null && x.SamAccountName != string.Empty)
                        .OrderBy(g => g.SamAccountName)
                        .Select(x => $"{x.SamAccountName.ToUpper()} - ({x.Description})")
                        .Distinct());
                }

                stopwatch.Stop();
                log.Info($"Group membership for {user.SamAccountName} -> {user.DisplayName}; [{user.Description}]");
                log.Info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                foreach (string group in GroupNamesAndDescriptions)
                {
                    log.Info(group);
                }
                log.Info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                log.Info($"Active Directory info gathering time elapsed: {stopwatch.Elapsed.TotalSeconds:00.00}s");

                return(GroupMembershipResult);
            }
            catch (Exception x)
            {
                log.Error($"Get membership failed: {x.Message}");
                return(new List <string>());
            }
        }
Beispiel #4
0
    private void BuildHList(GroupPrincipal node, int level, GroupPrincipal parent)
    {
        PrincipalSearchResult <Principal> rslts = node.GetMembers();

        _groups.Add(new GroupObj()
        {
            Group = node, Level = level, Parent = parent
        });
        foreach (GroupPrincipal grp in rslts.Where(g => g is GroupPrincipal))
        {
            BuildHList(grp, level + 1, node);
        }
    }
Beispiel #5
0
        /// <summary>
        /// Filtrage par Groupe
        /// </summary>
        private bool AdFilter(JToken alert)
        {
            Boolean userInGroup = false;
            Boolean liste       = false;
            string  currentUser = KnownUserProperties.AccountName;

            PrincipalSearchResult <Principal> groups = UserPrincipal.Current.GetGroups();
            IEnumerable <string> groupNames          = groups.Where(x => x.SamAccountName.Equals(alert["adGroups"])).Select(x => x.SamAccountName);

            if (alert["adGroups"] != null)
            {
                liste = true;
            }

            if (groupNames.Count() > 0 && liste == true)
            {
                userInGroup = true;
            }


            return(userInGroup);
        }
 private void BuildHList(GroupPrincipal node, int level, GroupPrincipal parent)
 {
     try
     {
         PrincipalSearchResult <Principal> rslts = node.GetMembers();
         _groups.Add(new GroupObj()
         {
             Group = node, Level = level, Parent = parent
         });
         try
         {
             foreach (GroupPrincipal grp in rslts.Where(g => g is GroupPrincipal))
             {
                 BuildHList(grp, level + 1, node);
             }
         }
         catch { }
     }
     catch (Exception ex)
     {
         throw new Exception(ex.ToString());
     }
 }