Solved

How to get Get usergroup of user from LDAP active directory

Posted on 2011-03-09
8
1,150 Views
Last Modified: 2013-12-24
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
Comment
Question by:Arslan306
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 11

Expert Comment

by:madgino
ID: 35088512
0
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35088633
0
 

Author Comment

by:Arslan306
ID: 35099120
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 6

Expert Comment

by:jwilleke
ID: 35115541
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
 

Author Comment

by:Arslan306
ID: 35147932
Still Its Not Working For Me. Can You Please Specify Any Other Solution
0
 
LVL 11

Expert Comment

by:madgino
ID: 35166031
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
 

Accepted Solution

by:
Arslan306 earned 0 total points
ID: 35706740
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
 

Author Closing Comment

by:Arslan306
ID: 35735891
Its Worked For Me
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Query Works in SQL 2008 & 2012 But Not SQL 2016 15 100
C# DateTime Help 6 46
SharePoint 2013 Search not working 9 72
Access VBA for Search Engine 7 9
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question