如何有效地了解AD组中是否存在用户

时间:2014-10-20 08:43:52

标签: c# asp.net sharepoint directoryservices

在sharepoint解决方案中,我需要从AD组中获取所有用户,目前这个AD组可以拥有30个用户,但将来,我们可以将AD组替换为具有1000个用户的AD组。并且因为在每个请求上为每个用户执行此代码(它是显示/隐藏OneDrive链接的导航组件),所以我需要它尽可能高效。

// 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?

我怎样才能尽快做到这一点?

1 个答案:

答案 0 :(得分: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);