public bool UserExistsInGroup(LdapObject domainGroup, LdapObject domainUser, LdapSettings settings) // string memberString, string groupAttribute, string primaryGroupId) { try { if (domainGroup == null || domainUser == null) { return(false); } var memberString = domainUser.GetValue(Settings.UserAttribute) as string; if (string.IsNullOrEmpty(memberString)) { return(false); } var groupAttribute = settings.GroupAttribute; if (string.IsNullOrEmpty(groupAttribute)) { return(false); } var userPrimaryGroupId = domainUser.GetValue(LdapConstants.ADSchemaAttributes.PRIMARY_GROUP_ID) as string; if (!string.IsNullOrEmpty(userPrimaryGroupId) && domainGroup.Sid.EndsWith("-" + userPrimaryGroupId)) { // Domain Users found return(true); } else { var members = domainGroup.GetValues(groupAttribute); if (!members.Any()) { return(false); } if (members.Any(member => memberString.Equals(member, StringComparison.InvariantCultureIgnoreCase) || member.Equals(domainUser.DistinguishedName, StringComparison.InvariantCultureIgnoreCase))) { return(true); } } } catch (Exception e) { Log.ErrorFormat("UserExistsInGroup() failed. Error: {0}", e); } return(false); }
protected bool CheckGroupNameAttribute(LdapObject group, string groupAttr) { try { var groupNameAttribute = group.GetValues(groupAttr); if (!groupNameAttribute.Any()) { _log.DebugFormat("Group Name Attribute parameter ({0}) not found: {1}", Settings.GroupNameAttribute, groupAttr); return(false); } } catch (Exception e) { _log.ErrorFormat("Group Attribute parameter ({0}) not found: {1}. {2}", Settings.GroupNameAttribute, groupAttr, e); return(false); } return(true); }