Solved

How to get Get usergroup of user from LDAP active directory

Posted on 2011-03-09
8
1,088 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
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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
compre toata in where clue oracle 4 41
Managing SQL log files, SQL Server 2014 6 55
Chat Room 1 26
ASP Button to clear text 4 18
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now