Search code examples
c#.netvisual-studiopostman

My Register API does not work when testing through postman: "Status Code: 400; Bad Request"


When Testing, I have attempted to match the input from my RegisterViewModel, no matter what different input I have tried using in postman to post the user it does not work, unsure if it's something to do with the method or my input in postman.

The RegisterViewModel looks like this:

public class RegisterViewModel
{
        [Required]
        public string Username { get; set; }
        [Required, DataType(DataType.Password)]
        public string Password { get; set; }
        [Required, DataType(DataType.Password)]
        public string ReEnterPassword { get; set; }
        public string Email { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
}

Heres my attempted input into postman:

{
 "username" : "user01",
 "password" : "test123",
 "reEnterPassword" : "test123",
 "email" : "user01@example.com",
 "firstName" : "Joe",
 "lastName" : "Bloggs"
}

The method being used:

[HttpPost("api/auth/register")]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> NotWrkingRegister([FromBody] RegisterViewModel user)
{

        if (user.Password != user.ReEnterPassword)
        {
            ModelState.AddModelError(string.Empty, "Password don't match");
            return BadRequest();
        }

        try
        {

            var newUser = Mapper.Map<RegisterViewModel, AppUser>(user);

            newUser.UserName = user.Username;

            newUser.APIKey = Guid.NewGuid();
            //newUser.Email = user.Email;

            var userCreationResult = await userManager.CreateAsync(newUser, user.Password);
            if (!userCreationResult.Succeeded)
            {
                foreach (var error in userCreationResult.Errors)
                    ModelState.AddModelError(string.Empty, error.Description);
                return View(user);
            }
            else
            {

                string confirmationToken = userManager.
                       GenerateEmailConfirmationTokenAsync(newUser).Result;

                string confirmationLink = Url.Action("ConfirmEmail",
                  "Account", new
                  {
                      userid = newUser.Id,
                      token = confirmationToken
                  },
                   protocol: HttpContext.Request.Scheme);
                var callbackUrl = Url.Action("confirmEmail", "api/auth",
                                   new { userId = newUser.Id.ToString(), code = confirmationToken },
                                   protocol: HttpContext.Request.Scheme);
                callbackUrl = $"http://localhost:49181/api/auth/confirmemail?userId=" + newUser.Id.ToString() + $"&token={confirmationToken}";


                 SendEmail(callbackUrl, newUser);



            }

            UserViewModel _userVM =
                  Mapper.Map<AppUser, UserViewModel>(newUser);
            return new OkObjectResult(_userVM);
        }
        catch (Exception ex)
        {
            _logger.LogError($"Exception thrown while creating JWT: {ex}");
        }
        return BadRequest();
}

Solution

  • Working now, there were duplicated values already registered within the database. After commenting out the[ValidateAntiForgeryToken] and running through the code break pointing each individual component, found the problem and rectified and got desired output.