In a sharepoint solution I need to get all users from an AD Group, for the time being this AD group can have 30 users, but in the future, we could replace the AD group with one that has 1000 users. and because this code is executed for each user on each request (its a navigation component to show/hide the OneDrive Link), then I need it to be as efficient as possible.
// 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?
How can I do this as fast as possible?
Warning, NOT tested.
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);