public async Task <IActionResult> TestLDAPAsync([FromForm] LdapSettings ldapConfig) { // Check authorization if (!(await User.IsAdminAsync(_authorizationService))) { return(Unauthorized()); } _logger.LogDebug("Testing LDAP for config: {0}", ldapConfig); int userCount = 0; int groupCount = 0; try { // Combine host, port and protocol to a single string string ldapStr = _ldapService.GetLdapStr(ldapConfig); // Try to connect if (!_ldapService.TestConnection(ldapStr)) { _logger.LogWarning("LDAP connection test failed"); return(Ok(new FailureResponse("Connection test failed: Check hostname and credientials"))); } // Count entries userCount = _ldapService.GetUserCount(ldapConfig); groupCount = _ldapService.GetGroupCount(ldapConfig); // Test user attributes if (userCount > 0 && !_ldapService.TestUserAttributes(ldapConfig)) { _logger.LogWarning("User attribute test failed"); return(Ok(new FailureResponse("User attributes not found"))); } // Test group attributes if (groupCount > 0 && !_ldapService.TestGroupAttrbutes(ldapConfig)) { _logger.LogWarning("Group attribute test failed"); return(Ok(new FailureResponse("Group attributes not found"))); } } catch (Exception e) { _logger.LogWarning(e, "Error while LDAP test"); return(Ok(new ErrorRespose(e))); } // Return success message _logger.LogDebug("LDAP test successfull. Found {0} users and {1} groups.", userCount, groupCount); return(Ok(new StringValueRespose($"Found {userCount} users and {groupCount} groups"))); }