2

I have a asp.net core server which, in this case, has been intentionally sent a fetch that produces a bad request response.

Here is part of the controller that does that:

    [HttpPost]
    [AllowAnonymous]
    public async Task<IActionResult> Get([FromBody] LoginViewModel model)
    {
        MembershipContext _userContext = _tokenService.ValidateUser(model.Email, model.Password);

        if (_userContext.Principal == null)
        {
            logger.LogInformation($"Invalid username ({model.Email}) or password ({model.Password})");
            return BadRequest("Invalid credentials"); // Here..
        }...

The bad request has "Invalid credentials" as part of the return..

On the browser and from where I do the fetch I get the response.status as "400" and "bad request" all good however I cannot find where I can check for "Invalid credentials" - can't find where it is.

Here is my fetch...

    // Lets do a fetch!
    const task = fetch("/api/jwt", {
        method: "POST",
        body: JSON.stringify(this.userLogin),
        headers: new Headers({ 'content-type': 'application/json' })
    })
        .then(response => {
            console.log("STATUS: ", response.status, response.statusText)
            return response.json()})
        .then(data => {
            console.log("got here", data)
            localStorage.setItem(this.TOKEN_KEY, JSON.stringify(data));
        })
        .then(() => {... so on..

How do I check the response for "Invalid Credentials"? I can check for bad response but it would be better to check what the server actually sent with that..

si2030
  • 3,895
  • 8
  • 38
  • 87

0 Answers0