public override List <LdapObject> GetGroups(Criteria criteria = null) { var list = new List <LdapObject>(); try { if (!string.IsNullOrEmpty(Settings.GroupFilter) && !Settings.GroupFilter.StartsWith("(") && !Settings.GroupFilter.EndsWith(")")) { Settings.GroupFilter = string.Format("({0})", Settings.GroupFilter); } var searchfilter = criteria == null ? Settings.GroupFilter : string.Format("(&{0}{1})", Settings.GroupFilter, criteria); list = LDAPSearcher.Search(Settings.GroupDN, NovellLdapSearcher.LdapScope.Sub, searchfilter); } catch (Exception e) { Log.ErrorFormat("NovellLdapHelper->GetGroups(criteria: '{0}') failed. Error: {1}", criteria, e); } return(list); }
public override List <LdapObject> GetUsers(string filter = null, int limit = -1) { var list = new List <LdapObject>(); try { if (!string.IsNullOrEmpty(Settings.UserFilter) && !Settings.UserFilter.StartsWith("(") && !Settings.UserFilter.EndsWith(")")) { Settings.UserFilter = string.Format("({0})", Settings.UserFilter); } if (!string.IsNullOrEmpty(filter) && !filter.StartsWith("(") && !filter.EndsWith(")")) { filter = string.Format("({0})", Settings.UserFilter); } var searchfilter = string.IsNullOrEmpty(filter) ? Settings.UserFilter : string.Format("(&{0}{1})", Settings.UserFilter, filter); list = LDAPSearcher.Search(Settings.UserDN, NovellLdapSearcher.LdapScope.Sub, searchfilter, limit: limit); return(list); } catch (Exception e) { Log.ErrorFormat("NovellLdapHelper->GetUsers(filter: '{0}' limit: {1}) failed. Error: {2}", filter, limit, e); } return(list); }
public override LdapObject GetUserBySid(string sid) { try { var ldapUniqueIdAttribute = ConfigurationManagerExtension.AppSettings["ldap.unique.id"]; Criteria criteria; if (ldapUniqueIdAttribute == null) { criteria = Criteria.Any( Expression.Equal(LdapConstants.RfcLDAPAttributes.ENTRY_UUID, sid), Expression.Equal(LdapConstants.RfcLDAPAttributes.NS_UNIQUE_ID, sid), Expression.Equal(LdapConstants.RfcLDAPAttributes.GUID, sid), Expression.Equal(LdapConstants.ADSchemaAttributes.OBJECT_SID, sid) ); } else { criteria = Criteria.All(Expression.Equal(ldapUniqueIdAttribute, sid)); } var searchfilter = string.Format("(&{0}{1})", Settings.UserFilter, criteria); var list = LDAPSearcher.Search(Settings.UserDN, NovellLdapSearcher.LdapScope.Sub, searchfilter, limit: 1); return(list.FirstOrDefault()); } catch (Exception e) { Log.ErrorFormat("NovellLdapHelper->GetUserBySid(sid: '{0}') failed. Error: {1}", sid, e); } return(null); }
public override bool CheckGroupDn(string groupDn) { string[] attributes = { LdapConstants.ADSchemaAttributes.OBJECT_CLASS }; var searchResult = LDAPSearcher.Search(groupDn, NovellLdapSearcher.LdapScope.Base, LdapConstants.OBJECT_FILTER, attributes, 1); if (searchResult.Any()) { return(true); } Log.ErrorFormat("NovellLdapHelper->CheckGroupDn(groupDn: {0}): Wrong Group DN parameter", groupDn); return(false); }
public override string SearchDomain() { try { var capabilities = GetCapabilities(); if (capabilities.Any()) { if (capabilities.ContainsKey("defaultNamingContext")) { var dnList = capabilities["defaultNamingContext"]; var dn = dnList.FirstOrDefault(dc => !string.IsNullOrEmpty(dc) && dc.IndexOf("dc=", StringComparison.InvariantCultureIgnoreCase) != -1); var domain = LdapUtils.DistinguishedNameToDomain(dn); if (!string.IsNullOrEmpty(domain)) { return(domain); } } if (capabilities.ContainsKey("rootDomainNamingContext")) { var dnList = capabilities["rootDomainNamingContext"]; var dn = dnList.FirstOrDefault(dc => !string.IsNullOrEmpty(dc) && dc.IndexOf("dc=", StringComparison.InvariantCultureIgnoreCase) != -1); var domain = LdapUtils.DistinguishedNameToDomain(dn); if (!string.IsNullOrEmpty(domain)) { return(domain); } } if (capabilities.ContainsKey("namingContexts")) { var dnList = capabilities["namingContexts"]; var dn = dnList.FirstOrDefault(dc => !string.IsNullOrEmpty(dc) && dc.IndexOf("dc=", StringComparison.InvariantCultureIgnoreCase) != -1); var domain = LdapUtils.DistinguishedNameToDomain(dn); if (!string.IsNullOrEmpty(domain)) { return(domain); } } } } catch (Exception e) { Log.WarnFormat("NovellLdapHelper->SearchDomain() failed. Error: {0}", e); } try { var searchResult = LDAPSearcher.Search(Settings.UserDN, NovellLdapSearcher.LdapScope.Sub, Settings.UserFilter, limit: 1) .FirstOrDefault(); return(searchResult != null?searchResult.GetDomainFromDn() : null); } catch (Exception e) { Log.WarnFormat("NovellLdapHelper->SearchDomain() failed. Error: {0}", e); } return(null); }