Solved

How to get Get usergroup of user from LDAP active directory

Posted on 2011-03-09
8
1,140 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…

713 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