Avatar of jculkincys
jculkincysFlag for United States of America

asked on 

Get user's LDAP attribute with C# in asp.net

I want to connect to a Sun LDAP server using C# in asp.net and check to see if a given user has a specific LDAP attribute. Below is the code that I have so far but its not working.

The netid variable is set to a string that contains the username
DirectoryEntry de = new DirectoryEntry("LDAP://myldapserver/o=my1sto,o=my2ndo,ou=People");
	de.Username = "user";
	de.Password = "password";
                DirectorySearcher searcher = new DirectorySearcher(de);
		searcher.Filter = "(uid=" + netid + ")";
		searcher.SearchScope = SearchScope.Subtree;
                // I want to see if the given user has the pdsRole attribute set to a specific value - lets sat "TEST"
		searcher.PropertiesToLoad.Add("pdsRole");
 
		SearchResult oResult = searcher.FindOne();
	
// its from here that I get confused
// am I using the SearchResult  object class right?
// How do I extract values from it and check them

Open in new window

.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
CB_Thirumalai
Avatar of CB_Thirumalai
CB_Thirumalai
Flag of India image

Hi, try the one below.  You may use try, catch to get any exception if you have
if (sr != null)
{
    // Get user's security and distribution groups
    string groups = GetGroups(sr);
 
    if (groups.Length > 0) {
        if (groups.ToLower().IndexOf("<your preferred group>") >= 0) {
               // do something
        } else {
            // do else
        }
    }
}

Open in new window

Avatar of jculkincys
jculkincys
Flag of United States of America image

ASKER

I get the following error

CS0103: The name 'GetGroups' does not exist in the current context
ASKER CERTIFIED SOLUTION
Avatar of CB_Thirumalai
CB_Thirumalai
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of CB_Thirumalai
CB_Thirumalai
Flag of India image

Have you got the solution for this?
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo