2012-01-31 25 views

回答

1

我相信你應該可以在S.DS.AM中做到這一點。 UserPrincipal以及GroupPrincipal最終都會從Principal下降 - 因此,如果您將「通用」主體傳遞給搜索者,則應該找回用戶和組(以及計算機)。

唯一棘手的部分是Principal是一個抽象類,所以你不能直接實例 - 你需要獲得一個第一UserPrincipal和「提取」從通用Principal

// set up dummy UserPrincipal 
UserPrincipal qbeUser = new UserPrincipal(ctx); 

// get the generic Principal from that - set the "Name" to search for 
Principal userOrGroup = qbeUser as Principal; 
userOrGroup.Name = "SomeName"; 

// create a PrincipalSearcher based on that generic principal 
PrincipalSearcher searcher = new PrincipalSearcher(userOrGroup); 

// enumerate the results - you need to check what kind of principal you get back 
foreach (Principal found in searcher.FindAll()) 
{ 
    // is it a UserPrincipal - do what you need to do with that... 
    if (found is UserPrincipal) 
    { 
     ...... 
    } 
    else if (found is GroupPrincipal) 
    { 
     // if it's a group - do whatever you need to do with a group.... 
    } 
} 
相關問題