public IList <KeyValuePair <string, string> > GetEntryAttributes(LdapDomain ldapDomain, string searchDn) { List <KeyValuePair <string, string> > _attributes = new List <KeyValuePair <string, string> >(); IList <LdapNodeObject> entries = new List <LdapNodeObject>(); var ldapConn = GetConnection(ldapDomain); var entry = ldapConn.Read(searchDn); // Get the attribute set of the entry LdapAttributeSet attributeSet = entry.getAttributeSet(); System.Collections.IEnumerator ienum = attributeSet.GetEnumerator(); // Parse through the attribute set to get the attributes and //the corresponding values while (ienum.MoveNext()) { LdapAttribute attribute = (LdapAttribute)ienum.Current; string attributeName = attribute.Name; string attributeVal = attribute.StringValue; if (attributeName == "objectGUID") { attributeVal = new Guid((Byte[])(Array)attribute?.ByteValue).ToString(); } _attributes.Add(new KeyValuePair <string, string>(attributeName, attributeVal)); } return(_attributes); }
public LdapConnection GetConnection(LdapDomain ldapDomain) { LdapConnection ldapConn = _conn as LdapConnection; int port; if (!ldapDomain.UseSsl) { port = LdapConnection.DEFAULT_PORT; } else { port = LdapConnection.DEFAULT_SSL_PORT; } if (ldapConn == null) { try { ldapConn = new LdapConnection() { SecureSocketLayer = false }; //Connect function will create a socket connection to the server - Port 389 for insecure and 3269 for secure ldapConn.Connect(ldapDomain.Host, port); //Bind function with null user dn and password value will perform anonymous bind to LDAP server //First figure the user structure string lpdaUser = ""; if (ldapDomain.User.Contains(@"\") || (ldapDomain.User.Contains("\\"))) { lpdaUser = ldapDomain.User; } else { lpdaUser = $@"{ldapDomain.Domain}\{ldapDomain.User}"; } ldapConn.Bind(lpdaUser, ldapDomain.Password); } catch (Exception ex) { throw ex; } } return(ldapConn); }
public async Task <IActionResult> LdapTestConnection(LdapDomain ldapDomain) { var connection = new LdapConnection(); var message = string.Empty; var result = false; try { //Get directory contents (nodesObjects) connection = (LdapConnection)_ldapManager.GetConnection(ldapDomain); result = connection.Connected; } catch (Exception ex) { message = ex.Message; } return(new JsonResult(new { connected = result, message = message }, new Newtonsoft.Json.JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() })); }
public async Task <IActionResult> SaveLdapSetting(LdapDomain ldap) { if (!string.IsNullOrEmpty(ldap.Id)) { var savedLdap = await _directoryManager.GetDirectoryLdapAsync(ldap.DirectoryId); savedLdap.Host = ldap.Host; savedLdap.Domain = ldap.Domain; savedLdap.User = ldap.User; savedLdap.Password = ldap.Password; savedLdap.LdapBaseDn = ldap.LdapBaseDn; savedLdap.UseSsl = ldap.UseSsl; _directoryManager.UpdateDirectoryLdapAsync(savedLdap); } else { _directoryManager.SaveDirectoryLdapAsync(ldap); } return(Ok(ldap)); }
public ICollection <LdapNodeObject> GetDirectoryEntries(LdapDomain ldapDomain, string searchBase) { IList <LdapNodeObject> entries = new List <LdapNodeObject>(); var ldapConn = GetConnection(ldapDomain); var filter = $"(objectClass=*)"; var scope = LdapConnection.SCOPE_ONE; var search = ldapConn.Search(searchBase, scope, filter, null, false); while (search.hasMore()) { try { var nextEntry = search.next(); var nodeObject = new LdapNodeObject { DistinguishedName = nextEntry.DN, Id = nextEntry.DN, ObjectGuid = GetEntryAttribute(nextEntry, "objectGuid"), OU = GetEntryAttribute(nextEntry, "ou"), Name = GetEntryAttribute(nextEntry, "name"), HasChildren = true //TODO figure out if node has children. }; entries.Add(nodeObject); } catch (LdapException e) { Console.WriteLine("Error: " + e.LdapErrorMessage); //Exception is thrown, go for next entry continue; } } return(entries); }
public async Task <IViewComponentResult> InvokeAsync(string id = null) { //var x = _ldapManager.SearchForGroup("MPCS_Groups"); var viewPage = "Default"; var ldap = new LdapDomain(); if (!string.IsNullOrEmpty(id)) { var objectId = id.Split('_'); var resourceType = objectId[0]; var directoryId = objectId[1]; ldap = await _directoryManager.GetDirectoryLdapAsync(directoryId) ?? new LdapDomain() { DirectoryId = directoryId }; ViewData["id"] = directoryId; } return(View("CorpLdapConnection", ldap)); }
public void SaveDirectoryLdapAsync(LdapDomain ldap) { _db.LdapDomains.Add(ldap); _db.SaveChanges(); }
public void UpdateDirectoryLdapAsync(LdapDomain ldap) { _db.Update(ldap); _db.SaveChanges(); }