ActiveDirectory - Obtenir le(s) groupe(s) auquel(s) un utilisateur appartient

Contenu du snippet

public static List<String> GetUserMemberOf(string domain, string username, string password) 
{
DirectorySearcher searcher = null; 
List<string> lMembers = new List<string>();
try


{
searcher = new DirectorySearcher(new DirectoryEntry(string.Concat(@"LDAP://", domain), username, password)); 
searcher.Filter = string.Concat(@"(&(objectClass=User) (sAMAccountName=", username, "))");searcher.PropertiesToLoad.Add(
@"MemberOf"); 
SearchResult result = searcher.FindOne();
for (int i = 0; i < result.Properties[@"MemberOf"].Count; i++) 
{
string szProp = result.Properties[@"MemberOf"][i].ToString();lMembers.Add(szProp.Substring(3, szProp.IndexOf(@",") - 3)); 
}

}
catch (Exception) 
{
throw; 
}

finally

{

searcher.Dispose();

}

return lMembers;


Compatibilité : C# 2.x

Disponible dans d'autres langages :

A voir également