protected virtual async Task <LdapConnection> CreateLdapConnection(Tenant tenant) { var ldapConnection = new LdapConnection(); ldapConnection.Connect(await _settings.GetDomain(tenant?.Id), await _settings.GetPort(tenant?.Id)); ldapConnection.Bind(await _settings.GetUserName(tenant?.Id), await _settings.GetPassword(tenant?.Id)); return(ldapConnection); }
protected virtual async Task <PrincipalContext> CreatePrincipalContext(TTenant tenant) { return(new PrincipalContext( await _settings.GetContextType(tenant?.Id), ConvertToNullIfEmpty(await _settings.GetDomain(tenant?.Id)), ConvertToNullIfEmpty(await _settings.GetContainer(tenant?.Id)), ConvertToNullIfEmpty(await _settings.GetUserName(tenant?.Id)), ConvertToNullIfEmpty(await _settings.GetPassword(tenant?.Id)) )); }
private async Task <bool> ValidateCredentialsAsync(string uid, TTenant tenant) { _logger.Info("ValidateCredentialsAsync against ldap host"); int ldapPort = await _settings.GetLdapServerPort(tenant?.Id); string ldapHost = await _settings.GetLdapHost(tenant?.Id); var loginDN = await _settings.GetLdapLoginDn(tenant?.Id); var loginPassword = await _settings.GetPassword(tenant?.Id); var ldapSearchBase = await _settings.GetLdapUserSearchBase(tenant?.Id); string searchLdapUser = uid; string searchFilter = "(objectclass=*)"; string searchBase = $"uid={searchLdapUser}, {ldapSearchBase}"; // "ou = scientists, dc = example, dc = com"; //"uid=gauss, dc=example, dc=com"; LdapSearchConstraints constraints = new LdapSearchConstraints { }; try { using (var cn = new LdapConnection()) { // connect cn.Connect(ldapHost, ldapPort); cn.Bind(loginDN, loginPassword); LdapSearchResults searchResults = cn.Search( searchBase, LdapConnection.SCOPE_SUB, searchFilter, null, // no specified attributes false, // false = return attr and value constraints); while (searchResults.HasMore()) { if (searchResults.Count == 1) { LdapEntry nextEntry = null; try { nextEntry = searchResults.Next(); } catch (LdapException e) { _logger.Error("Error: " + e.LdapErrorMessage); //Exception is thrown, go for next entry continue; } LdapEntries = new Dictionary <string, string>(); _logger.Debug(nextEntry.DN); // Get the attribute set of the entry LdapAttributeSet attributeSet = nextEntry.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; _logger.Debug(attributeName + "value:" + attributeVal); LdapEntries.Add(attributeName, attributeVal); } return(true); } } } } catch (LdapException ldapEx) { throw new AbpException(ldapEx.ToString()); // ocassional time outs } catch (Exception ex) { throw new AbpException(ex.ToString()); } return(false); }