• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

add a username to check in the follow function.

I would like to modifiy the function below and check for the username in the group of members.

            string fullname = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
            string[] parts = fullname.Split('\\');
            string sDomain = parts[0];
            string sUser = parts[1];
            string sGroup = "gl ca corporate data";
            StringCollection groupMembers = this.GetGroupMembers(sDomain, sGroup, sUser);

I have a function that gets group members

 public StringCollection GetGroupMembers(string strDomain, string strGroup,  ?)
            StringCollection groupMemebers = new StringCollection();
                DirectoryEntry ent = new DirectoryEntry(_path);
                DirectorySearcher srch = new DirectorySearcher("(CN=" + strGroup + ")");
                SearchResultCollection coll = srch.FindAll();
                foreach (SearchResult rs in coll)
                    ResultPropertyCollection resultPropColl = rs.Properties;
                    foreach (Object memberColl in resultPropColl["member"])
                        DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);
                        System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;
                        object obVal = userProps["sAMAccountName"].Value;
                        if (null != obVal)
            catch (Exception ex)
            return groupMemebers;
1 Solution
You can use this code.
bool isUserInRole = groupMembers.Contains(sUser);

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now