Asked By
zanyap
0 points
N/A
Posted on - 10/31/2011
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!
Thanks
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.
"Group1|Group2|Group3|"
To develop LDAP group retrieval code to look up the user's group membership
-
Add the following implementation of the GetGroups method to the LdapAuthentication class.
 public string GetGroups()
 {
  DirectorySearcher search = new DirectorySearcher(_path);
  search.Filter = "(cn=" + _filterAttribute + ")";
  search.PropertiesToLoad.Add("memberOf");
  StringBuilder groupNames = new StringBuilder();
  try
  {
  SearchResult result = search.FindOne();
  int propertyCount = result.Properties["memberOf"].Count;
  String dn;
  int equalsIndex, commaIndex;
  for( int propertyCounter = 0; propertyCounter < propertyCount;
    propertyCounter++)
  {
   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));
   groupNames.Append("|");
  }
 }
 catch(Exception ex)
 {
  throw new Exception("Error obtaining group names. " +
   ex.Message);
 }
 return groupNames.ToString();
}
This will help to solve your query.