Help with this Code Please

Asked By 0 points N/A Posted on -

Hi everyone ,

I am creating an application in ASP.NET Web application in Visual C# .NET particularly authentication.

I read a forum on the internet and I follow the steps and the source code and its working fine using LdapAuthentication.

But my issue here is that I need to call a certain AD group in the system by using GetGroups method.

I need a code that will tell that into the system. Please help!


Answered By 0 points N/A #98456

Help with this Code Please


Please go through the below mentioned codes to use the GetGroup method to call a certain AD group in the system.


To develop LDAP group retrieval code to look up the user's group membership

  1. Add the following implementation of the GetGroups method to the LdapAuthentication class.

 public string GetGroups()


    DirectorySearcher search = new DirectorySearcher(_path);

    search.Filter = "(cn=" + _filterAttribute + ")";


    StringBuilder groupNames = new StringBuilder();



   SearchResult result = search.FindOne();

    int propertyCount = result.Properties["memberOf"].Count;

    String dn;

    int equalsIndex, commaIndex;

   for( int propertyCounter = 0; propertyCounter < propertyCount;



     dn = (String)result.Properties["memberOf"][propertyCounter];

    equalsIndex = dn.IndexOf("=", 1);

     commaIndex = dn.IndexOf(",", 1);

     if (-1 == equalsIndex)


        return null;


     groupNames.Append(dn.Substring((equalsIndex + 1),

                       (commaIndex – equalsIndex) – 1));




 catch(Exception ex)


   throw new Exception("Error obtaining group names. " +



  return groupNames.ToString();


This will help to solve your query.

Login/Register to Answer

Related Questions