2009-02-04 6 views
6

Je souhaite obtenir les appartenances à un groupe d'utilisateurs dans un annuaire ActiveDirectory, sans appartenir au domaine. Quand je cours cela dans le domaine, tout va bien.Demander les rôles d'un utilisateur dans AD lorsque l'appelant n'est pas dans le domaine

var context = new PrincipalContext(ContextType.Domain); 
var principal = UserPrincipal.FindByIdentity(context, IdentityType.Name, "administrator"); 

foreach (var authorizationGroup in principal.GetAuthorizationGroups()) 
{ 
    Console.WriteLine(authorizationGroup.Name); 
} 

Cependant, quand je cours en dehors du domaine, je dois préciser le PrincipalContext se trouvent ceci:

var context = new PrincipalContext(ContextType.Domain, "10.0.1.255", "DC=test,DC=ad,DC=be", "administrator", "password"); 

Quand je lance ce code, je reçois une exception quand j'exécute principal.GetAuthorizationGroups(). L'exception que je reçois est:

System.DirectoryServices.AccountManagement.PrincipalOperationException: Information about the domain could not be retrieved (1355). 
at System.DirectoryServices.AccountManagement.Utils.GetDcName(String computerName, String domainName, String siteName, Int32 flags) 
at System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo() 
at System.DirectoryServices.AccountManagement.ADStoreCtx.get_DnsDomainName() 
at System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOfAZ(Principal p) 
at System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroupsHelper() 
at System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroups() 

Répondre

1

Cela ressemble à un problème DNS.

Le localisateur DC fonctionne en effectuant des requêtes DNS pour les enregistrements SRV afin de trouver le contrôleur de domaine approprié sur votre site actuel. Si ce n'est pas le cas, le localisateur DC échouera, ce qui se passe dans votre trace de pile.

+0

J'ai eu ce problème. En cours d'exécution "nslookup yourdomain" devrait résoudre à votre DC. – T3hc13h

0

Peut-être que je ne peux pas le vérifier maintenant.

J'ai essayé ce qui suit: J'utilise l'excellent Active DirectoryExplorer de sysinternals. Lorsque vous vous connectez avec les mêmes informations d'identification: 10.0.1.255, "administrator", "password"

Maintenant, je peux voir les groupes sans problèmes

["memberOf"] = "CN=TestGroup,CN=Users,DC=test,DC=ad,DC=be" 
2

de l'utilisateur que je viens d'avoir à traiter avec le même problème. J'espère que cela aidera quelqu'un d'autre.

/*Argument*/ 
string username; 



/*Global settings*/ 
string ADHost = "dc.a.b.c"; /*Or ip address*/ 
string ADUsername = "username"; 
string ADPassword = "password"; 
string ADDomain = "a.b.c"; 
string ADContainer = "DC=A,DC=B,DC=C"; /*I have a function to do the translation*/ 
/*Global settings*/ 

var list = new List<string>(); 

var path = "LDAP://" + ADHost + "/" + ADContainer; 
var deDomain = new DirectoryEntry(path, ADUsername, ADPassword); 
var ds = new DirectorySearcher(deDomain, "(&(objectClass=User)(sAMAccountName=" + username + "))"); 

ds.SearchScope = SearchScope.Subtree; /*Cascade*/ 
ds.ReferralChasing = ReferralChasingOption.All; /*Follow redirection*/ 

var usr = ds.FindOne(); 
if (null != usr) 
{ 
    var deUsr = new DirectoryEntry(usr.Path, ADUsername, ADPassword); 

    foreach (string groupDN in deUsr.Properties["memberOf"]) 
    { 
     string[] parts = groupDN.Replace("CN=", "").Split(','); 
     list.Add(parts[0]); 
    } 
} 
Questions connexes