Quelle est la version de .NET?
Si vous êtes sur .NET 3.5, voyez cet excellent MSDN article sur la façon dont l'interface Active Directory a beaucoup changé.
Si vous êtes sur .NET 3.5, vous pouvez écrire:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");
Principal myObject = Principal.FindByIdentity(ctx, "your name value");
En règle générale, il faudrait passer simplement le nom d'utilisateur - la partie après la barre oblique inverse - pas tout DOMAIN \ USERNAME chaîne.
Ce "principal" maintenant soit est un UserPrincipal
ou un GroupPrincipal
(ou il pourrait d'un autre type de capital, par exemple ComputerPrincipal
):
if(myObject is UserPrincipal)
{
// you have a user
}
else if(myObject is GroupPrincipal)
{
// you have a group
}
et vous pouvez à partir de là.
Si vous êtes sur .NET 1.x/2.0/3.0, vous devriez utiliser la procédure un peu plus impliqué de créer un DirectorySearcher
et la recherche de votre objet:
// create root DirectoryEntry for your search
DirectoryEntry deRoot = new DirectoryEntry("LDAP://dc=YourCompany,dc=com");
// create searcher
DirectorySearcher ds = new DirectorySearcher(deRoot);
ds.SearchScope = SearchScope.Subtree;
// define LDAP filter - all you can specify is the "anr" (ambiguous name
// resolution) attribute of the object you're looking for
ds.Filter = string.Format("(anr={0})", "YourNameValue");
// define properties you want in search result(s)
ds.PropertiesToLoad.Add("objectCategory");
ds.PropertiesToLoad.Add("displayName");
// search
SearchResult sr = ds.FindOne();
// check if we get anything back, and if we can check the "objectCategory"
// property in the search result
if (sr != null)
{
if(sr.Properties["objectCategory"] != null)
{
// objectType will be "Person" or "Group" (or something else entirely)
string objectType = sr.Properties["objectCategory"][0].ToString();
}
}
Marc
Merci pour votre réponse et votre rappel. – ddou