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> ValidateCredentials(string userNameOrEmailAddress, string plainPassword, Tenant tenant) { var searchFilter = await CreateSearchFilter(userNameOrEmailAddress, tenant); var attrs = new string[] { "sAMAccountName" }; try { using (var ldapConnection = await CreateLdapConnection(tenant, userNameOrEmailAddress)) { var entities = ldapConnection.Search(await _settings.GetContainer(tenant?.Id), LdapConnection.SCOPE_SUB, searchFilter, attrs, false); string userDn = null; while (entities.HasMore()) { var entity = entities.Next(); var account = entity.getAttribute("sAMAccountName"); if (account != null && account.StringValue == userNameOrEmailAddress) { userDn = entity.DN; break; } } if (string.IsNullOrWhiteSpace(userDn)) { return(false); } ldapConnection.Bind(userDn, plainPassword); ldapConnection.Disconnect(); return(true); } } catch (Exception) { return(false); } }