Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1172
  • Last Modified:

How to get Get usergroup of user from LDAP active directory

Hi All,

I want to check usergroup of user on LDAP active directory or you can say I want to check user is member of these groups on LDAP Active Directory. I will send user name to that and find my user "XYZ" is member of these groups. Can You Please provide me Code snippet for my .net ASP Application. Thanks in Advance
0
Arslan306
Asked:
Arslan306
1 Solution
 
Arslan306Author Commented:
Hi Madgino,
I have checked Your Code snippet, Its First Part is working fine But at this Line Code gives exception

deSearcher.PropertiesToLoad.Add("memberOf");
 SearchResult sResult = deSearcher.FindOne();  \\ Here Exception Comes

Exception Message is "Bad UserName And Password Or Unknown Error"

I Have checked multiple time With My Username And Password And Domain Name And Group Name.
All are Working Perfect. There is No Error in That. Please Test That On Your Side Too. Thanks Or Purpose Any Other Solution.
Thanks
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
jwillekeCommented:
Basics, form an LDAP standpoint, looking at AD, members of groups are expressed as values for the "member" attribute by the fully distinguished name of the members.

Looking at a group, the member attribute might show:

member: CN=Administrator,CN=Users,DC=mad,DC=willeke,DC=com
member: CN=jim,CN=Users,DC=mad,DC=willeke,DC=com

In AD, there is a server derived attribute, meaning the server puts the value there and you should no mofify the values,  "memberOf" that is on the user entry that indicates the fully distinghushed name of any groups the entry is a member of.

The code referenced by madgino is looks at the "memberOf" values. (Which should work fine)
The problem, I believe, you are having is in locating the entry.

The code uses this line to find the user:
deSearcher.Filter = "sAMAccountName=" + Username.Replace("@" + LDAPServer, "");

The samAccountName does NOT contain a @ sign. The userPrincipalname does.
Try changing this line to:
deSearcher.Filter = "sAMAccountName="+ Username;

-jim
0
 
Arslan306Author Commented:
Still Its Not Working For Me. Can You Please Specify Any Other Solution
0
 
madginoCommented:
Hi, sorry for the delay but I wanted to test it too before responding you. The below code is working fine for me, you just have to be sure when you're searching that you're using a property that exists in your LDAP.
For example the property sAMAccountName doesn't exists in my LDAP and the search was failing on FindOne method. After I replaced with uid it went fine.

DirectoryEntry root = new DirectoryEntry("LDAP://myserver/dc=v1,dc=v2");

root.AuthenticationType = AuthenticationTypes.None;

DirectorySearcher searcher = new DirectorySearcher(root, string.Format("(uid={0})", "username"));
            
SearchResult sr = searcher.FindOne();

if (!(sr == null)) 
{
	int groupCount = sr.Properties["memberOf"].Count - 1;
}

Open in new window

0
 
Arslan306Author Commented:
I found Solution Myself. Hope It will Help you in future.

Hi I Found The Solution Myself. Hope this will help You

public List<GroupPrincipal> GetGroupsForUser(string username)
    {
        List<GroupPrincipal> result = new List<GroupPrincipal>();

        // set up domain context - if you do a lot of requests, you might
        // want to create that outside the method and pass it in as a parameter
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain,"ElizaPurton");
        ctx.ValidateCredentials(txtUsername.Text, txtPassword.Text);


        // find user by name
        UserPrincipal user = UserPrincipal.FindByIdentity(ctx,username);

        // get the user's groups
        if (user != null)
        {
            foreach (GroupPrincipal gp in user.GetAuthorizationGroups())
            {
                result.Add(gp);
            }
        }

        return result;
    }
0
 
Arslan306Author Commented:
Its Worked For Me
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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