Example #1
0
        // Get which group they belong to
        public string getAuthorizationGrps(string userName)
        {
            List <string>    grps        = new List <string>();
            string           domain_name = GetSystemDomain();
            PrincipalContext context     = new PrincipalContext(ContextType.Domain, domain_name);;

            try
            {
                var currentUser = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName);
                RevertToSelf();
                PrincipalSearchResult <Principal> groups = currentUser.GetGroups();
                IEnumerable <string> groupNames          = groups.Select(x => x.SamAccountName);
                foreach (var name in groupNames)
                {
                    grps.Add(name.ToString());
                }
                string groupLists = string.Join(", ", grps);
                return(groupLists);
            }
            catch (Exception ex)
            {
                string error = ex.ToString();
                return(error);
            }
        }
Example #2
0
        public string GetUserGroups(string serverAddress, string domain, string userName, string password)
        {
            string result = "";

            try
            {
                //DirectoryEntry entry = new DirectoryEntry(serverAddress, userName, password);
                //DirectorySearcher searcher = new DirectorySearcher(entry.);

                PrincipalSearchResult <Principal> groups = UserPrincipal.Current.GetGroups();
                var displayName  = UserPrincipal.Current.DisplayName;
                var emailAddress = UserPrincipal.Current.EmailAddress;
                var authGroups   = UserPrincipal.Current.GetAuthorizationGroups();
                var ab           = UserPrincipal.Current.GetGroups();

                IEnumerable <string> groupNames = groups.Select(x => x.SamAccountName);
                foreach (var groupName in groupNames)
                {
                    result += "|" + groupName;
                }
                result = "";
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            return(result);
        }
Example #3
0
        //Invoke on UI Thread
        public void UpdateTextBox(int procName)
        {
            //Get AD groups membership for user

            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "HCA");
            UserPrincipal    usr = UserPrincipal.FindByIdentity(ctx, GetProcessOwner1(procName));
            PrincipalSearchResult <Principal> groups = usr.GetAuthorizationGroups();
            IEnumerable <string> groupNames          = groups.Select(x => x.SamAccountName);
            GroupPrincipal       spGroup             = default(GroupPrincipal);
            GroupPrincipal       spGroup2            = default(GroupPrincipal);

            //dockHlp = new Form2(GetActiveWindowTitle(GetForegroundWindow()), null);
            dynamic pName = Process.GetProcessesByName("T");

            foreach (Process procs in pName)
            {
                dockHlp = new Form2(procs.MainWindowTitle, null);
                System.Windows.Application.Current.Dispatcher.Invoke(       //Invoke hooks on main thread.
                    () =>
                {
                    ActivateMouseHook();
                    dockHlp.Subscribe();
                });
            }

            this.Dispatcher.Invoke(() =>
            {
                TextBox1.AppendText("User ID: " + GetProcessOwner1(procName) + "\r\n");
            });

            foreach (string group in groupNames)
            {
                this.Dispatcher.Invoke(() =>
                {
                    TextBox1.AppendText("Group: " + group + "\r\n");
                });
            }

            //RichTextBox1.AppendText("Mouse hooked=" & dock.HookMouse() & vbCrLf)

            //try
            //{
            //    //spGroup = GroupPrincipal.FindByIdentity(ctx, "CWDV_AppAdmin_HVAValueBar");
            //    ////CWDV_AppAdmin_HVAValueBar
            //    //spGroup2 = GroupPrincipal.FindByIdentity(ctx, "Administrators");
            //    //TextBox1.AppendText("User is a member of HVA_Group: " + usr.IsMemberOf(spGroup) + "\r\n");
            //    //TextBox1.AppendText("User is a member of Administrators: " + usr.IsMemberOf(spGroup2) + "\r\n");
            //}
            //catch
            //{
            //    // RichTextBox1.AppendText("AD Group NOT FOUND" & vbCrLf)
            //}

            //TextBox1.AppendText("UpdateTextBox");


            //RichTextBox1.AppendText("Initial Top: " & r.top & " Initial Bottom: " & r.bottom & " Initial Left: " & r.left & " Initial Right: " & r.right & vbCrLf &
            //       "Initial Width x Height: " & r.right - r.left & " X " & r.bottom - r.top & vbCrLf)
        }
Example #4
0
        /// <summary>
        /// This method returns the groups of which the principal is directly a member, recursive searches may be performed.
        /// Recursive search results are available for user principal objects. For more information, see the GetAuthorizationGroups method.
        /// </summary>
        public string[] GetGroups()
        {
            if (_groupArray != null)
            {
                return(_groupArray);
            }
            else
            {
                PrincipalSearchResult <Principal> groups = null;
                var appsettings   = ConfigurationManager.AppSettings;
                var userPrincipal = FindUserInAd();

                if (userPrincipal == null)
                {
                    _log.ErrorFormat("User '{0}' is not present in the AD-container(s) specified in Web.config", GetActiveUser());
                    throw new HttpException(404, "User not found");
                }

                try
                {
                    if (_recursiveSearch)
                    {
                        _log.Info("RecursiveSearch AD-search used");
                        groups = userPrincipal.GetAuthorizationGroups();
                    }
                    else
                    {
                        _log.Info("Non-recursive AD-search used");
                        groups = userPrincipal.GetGroups();
                    }
                }
                catch (Exception e)
                {
                    _log.ErrorFormat("Error getting groups for user '{0}', Error: {1}", GetActiveUser(), e.Message);
                }

                if (groups != null)
                {
                    _groupArray = groups.Select(g => g.Name).ToArray();
                }
                else
                {
                    _log.InfoFormat("No groups found for user '{0}', using empty group array", GetActiveUser());
                    _groupArray = new string[0];
                }

                if (_log.IsInfoEnabled)
                {
                    _log.Info("The active user is a member of the following groups:");
                    for (int i = 0; i < _groupArray.Length; i++)
                    {
                        _log.Info(_groupArray[i]);
                    }
                }

                return(_groupArray);
            }
        }
Example #5
0
        /// <summary>
        /// Retrieves all groups in computer/domain.
        /// </summary>
        /// <returns>Enumerable with groups.</returns>
        private IEnumerable <IHierarchyItemAsync> getGroups()
        {
            GroupPrincipal insGroupPrincipal = new GroupPrincipal(Context.GetPrincipalContext());

            insGroupPrincipal.Name = "*";
            PrincipalSearcher insPrincipalSearcher = new PrincipalSearcher(insGroupPrincipal);

            PrincipalSearchResult <Principal> r = insPrincipalSearcher.FindAll();

            return(r.Select(g => new Group((GroupPrincipal)g, Context)).Cast <IHierarchyItemAsync>().ToList());
        }
Example #6
0
    public static ADUser Find(string accountname)
    {
        ADUser user = new ADUser();

        if (!DoesUserExist(accountname))
        {
            user = null;
        }
        else
        {
            try
            {
                PrincipalContext ouContex = new PrincipalContext(ContextType.Domain, "TRR-INET.local", Utilities.GetSearchOU());

                UserPrincipal up = UserPrincipal.FindByIdentity(ouContex, accountname);

                if (up != null)
                {
                    user.AccountName = up.SamAccountName;
                    user.Firstname   = up.GivenName;
                    user.Lastname    = up.Surname;
                    user.Locked      = up.IsAccountLockedOut();
                    user.OU          = up.DistinguishedName.Substring(up.DistinguishedName.IndexOf(',') + 1);
                    user.up          = up;
                    user.DateExpires = up.AccountExpirationDate;
                    user.Enabled     = up.Enabled == null ? true : up.Enabled.Value;

                    user.Groups = new List <string>();
                    PrincipalSearchResult <Principal> usersGroups = up.GetGroups();
                    IEnumerable <string> groupNames = usersGroups.Select(x => x.SamAccountName);
                    foreach (var name in groupNames)
                    {
                        if (name.StartsWith("T-") || name.StartsWith("IT"))
                        {
                            user.Groups.Add(name.ToString());
                        }
                    }
                }
                else
                {
                    throw new NoAccessToADUserException();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        return(user);
    }
Example #7
0
        public IEnumerable <string> GetGroups(string username)
        {
            var a = WebUtility.UrlDecode(username);

            principalContext = new PrincipalContext(ContextType.Domain);
            UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, a);

            if (user is null)
            {
                throw new UserNotFoundException();
            }

            PrincipalSearchResult <Principal> groups = user.GetAuthorizationGroups();

            return(groups.Select(g => g.Name));
        }
Example #8
0
        /// <summary>
        /// Gets all the users in the ACTIVE_DIRECTORY_GROUP
        /// </summary>
        /// <returns>A list of users (Domain GUID and name)</returns>
        public List <AdUser> GetUsers()
        {
            // Create the List
            List <AdUser> users = new List <AdUser>();

            // Get connection to AD
            using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, ACTIVE_DIRECTORY_DOMAIN))
            {
                GroupPrincipal group = GroupPrincipal.FindByIdentity(principalContext, ACTIVE_DIRECTORY_GROUP);
                PrincipalSearchResult <Principal> groupusers = group.GetMembers();

                IEnumerable <UserPrincipal> groupusersagain = groupusers.Select(g => g as UserPrincipal);

                // Add users to list
                foreach (var user in groupusersagain)
                {
                    users.Add(new AdUser(user));
                }
            }
            return(users);
        }
Example #9
0
        internal static string[] GetGroups()
        {
            PrincipalSearchResult <Principal> groups = UserPrincipal.Current.GetGroups();

            return(groups.Select(x => x.SamAccountName).ToArray());
        }