Similar to the problem in the following MSDN thread: http://social.msdn.microsoft.com/Forums/en-MY/csharplanguage/thread/4c9fea6c-1d0a-4733-a8ac-e3b78d10e999
I'm trying to check if this user is a member of a group, and our existing functional solutions are too slow (13-16 seconds), and I'm trying to speed it up. I currently have:
public bool IsMemberAD(string userName, string groupName) { var pc = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain); var user = System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(pc, System.DirectoryServices.AccountManagement.IdentityType.SamAccountName, userName.ToLower()); var group = System.DirectoryServices.AccountManagement.GroupPrincipal.FindByIdentity(pc, groupName); if (group == null || user == null) return false; return user.IsMemberOf(group); }
What makes it interesting is that it returns an error only when the user is not in the group directly, but rather a member of the group included in the target group.
For instance:
Steve and Sam are two users, and GroupParent and GroupChild are two groups. Steve and GroupChild are members of GroupParent. Sam is a member of GroupChild. If I call this function (Steve, GroupParent), it returns true. If I call it (Sam, GroupParent), I get an error. If I call it ("fdkjskghkf", GroupParent), it will return false.
I linked the article above with similar problems, but its solution did not work for me, I still have the same error. Ideas?
source share