Question: I have C# client that's making requests to another Web API.

The Web API I'm consuming can respond with 2 "successful" responses, both 200's.

I need to intercept the response before I map it to its corresponding object and am looking for the best practice.

I own both the client and the server, so either can be adjusted to fit best practices.

I haven't tried much, but I've considered the following

1. Having an understood map of unclaimed status codes to each error.
2. Try-catching the attempt to map the JSON to either of the two business objects or otherwise parsing and comparing the JSON to the expected format each object expects before mapping it.

```
response = await PostAsync(
    $"{BaseUrl}/endpoint/{identifier}",
    new StringContent(jsonBody, Encoding.UTF8, "application/json")
);

var responseJson = await response.Content.ReadAsStringAsync();
var responseObject = json.Deserialize<ResponseObject>(responseJson);
businessObject = new BusinessObject(responseObject);```

//These are two example responses
{
 "StartDate": "01/01/01",
 "EndDate": "01/01/01"
 "Object": {
  "Property1": 1,
  "Property2": "someStr"
 }
}
//OR
{
 "message": "Valid reason you are not receiving the JSON above, status 200 btw"
}

```

Answer: 
I am also doing the same in my application:

* Authenticate against the LDAP
* Authenticate against the database.

First of all LDAP server is already set up.
So in application.properties file we have all the information required to connect to the LDAP server and **a Boolean field to check whether we want to authenticate against LDAP server or not.**

```
project.ldap.server.protocol=
project.ldap.server.host.name=
project.ldap.server.ip=
project.ldap.server.port=
project.ldap.service.url=
project.ldap.authentication=(false/true)

```

A service which will perform the authentication against the LDAP server

```
@Service
public class LDAPAuthenticationConnectorService {

    @Value("${project.ldap.server.protocol}")
    private String LDAP_SERVER_PROTOCOL;

    @Value("${project.ldap.server.ip}")
    private String LDAP_SERVER_IP;

    @Value("${project.ldap.server.port}")
    private int LDAP_SERVER_PORT;

    @Value("${project.ldap.service.url}")
    private String LDAP_SERVICE_URL;

    /**
     * 
     * @param loginDto
     * @throws ADAuthenticationException
     */
    public String authenticate(LoginDto loginDto) throws ADAuthenticationException{//logic }

```

Now in FacadeImplementation you can do as following:

```
 public class LoginFacadeImpl implements LoginFacadeInt {
       @Value("${project.ldap.authentication}")
        private Boolean isProjectLDAPAuthenticationEnabled;
    @Override
        public UserDto authenticateUser(String userName, String password) {
    try {
                authenticateViaLDAP(userName, password);
            } catch (Exception e1) {
                //do authetication against database
                UserEntity userEntityForAuthentication = 
                UserManagementDaoInt.authenticateUser(userName,
                        AuthenticationUtils.convertPasswordToSha256(password));
}

```

Hope this helps:)
Let me know:)
I found the solution. 

> 
> Spring Security supports a wide range of authentication mechanisms. AuthenticationManagerBuilder object allows using multiple built-in authentication provider like In-Memory authentication, LDAP authentication, JDBC based authentication. In addition to its own set of authentication models, Spring Security allows to write your custom authentication mechanism to authenticate, for example, against a secure RESTful or SOAP remote API authentication service.
> 
> 
> 

Link: <https://www.baeldung.com/spring-security-multiple-auth-providers>

The following example shows how to put two authentication providers, one in a row from another. One: in memory, and other: customAuthentcationProvider.

* auth.authenticationProvider(customAuthProvider);
* auth.inMemoryAuthentication()

Example:

```
@EnableWebSecurity
public class MultipleAuthProvidersSecurityConfig extends WebSecurityConfigurerAdapter                     {
@Autowired
CustomAuthenticationProvider customAuthProvider;

@Override
public void configure(AuthenticationManagerBuilder auth) 
  throws Exception {

    auth.authenticationProvider(customAuthProvider);
    auth.inMemoryAuthentication()
        .withUser("memuser")
        .password(encoder().encode("pass"))
        .roles("USER");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic()
        .and()
        .authorizeRequests()
        .antMatchers("/api/**")
        .authenticated();
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}
}

```