2014-10-20 98 views
-1

在分享點解決方案中,我需要從AD組獲得所有用戶,目前AD組可以有30個用戶,但將來我們可以替換AD一個擁有1000個用戶的組。並且因爲每個用戶在每個請求(其導航組件顯示/隱藏OneDrive鏈接)上都執行此代碼,所以我需要它儘可能高效。如何知道用戶是否在AD組中有效存在

// Get all users from a group recursively. 
         var context = new System.DirectoryServices.AccountManagement.PrincipalContext(ContextType.Domain); 
         GroupPrincipal group = new GroupPrincipal(context ,farm.Properties[GlobalNavigationConstants.Keys.GlobalNavigationOneDriveADGroup].ToString()); 
         PrincipalSearchResult<Principal> members = group.GetMembers(true); 
         var list = members.OfType<UserPrincipal>().ToList(); 

         //Get current user 
         var loginName = SPContext.Current.Web.CurrentUser.LoginName; 

         //How to check if loginname is on list efficiently? 

我該如何儘可能快地做到這一點?

回答

0

警告,未測試。

var context = new System.DirectoryServices.AccountManagement.PrincipalContext(ContextType.Domain); 
GroupPrincipal group = new GroupPrincipal(context, 
    farm.Properties[GlobalNavigationConstants.Keys.GlobalNavigationOneDriveADGroup].ToString()); 

UserPrincipal usr = UserPrincipal.FindByIdentity(context, 
              IdentityType.Sid, 
              SPContext.Current.Web.CurrentUser.Sid); 

var found = usr.IsMemberOf(group); 
相關問題