[SOLVED] Multiple objects to single endpoint with asp.net

Issue

I’m building a Restful API with asp.net core. I have an endpoint that are used to authenticate users. I have two kinds of Users where one is an Admin and one is "FilmStudio". I succesfully managed to authenticte the User (Admin) but i also need to be able to authenticate the FilmStudio with username and password. Is there anyway I can do this with a single endpoint?

This is my endpoint form the UsersController:

        [AllowAnonymous]
        [HttpPost("Authenticate")]
        public IActionResult Authenticate([FromBody] UserDto model)
        {

            var user = _userRepo.Authenticate(model.UserName, model.Password);

            if (user !=null)
            {
               
                if (user == null)
                {
                    return BadRequest("The username or password is incorrect.");
                }


                return Ok(new
                {
                    Id = user.UserId,
                    Username = user.UserName,
                    Role = user.Role,
                    Token = user.Token
                });

            }
            else
            {
                var filmStudioDto = new FilmStudioDto();
                var studio = _studioRepo.Authenticate(model.Name, model.Password);
                if (studio == null) 
                {
                    return BadRequest("The username or password is incorrect.");
                }

                return Ok(new
                {
                    Id = studio.StudioId,
                    Username = studio.Name,
                    City = studio.City,
                    Role = studio.Role,
                    Token = studio.Token
                });
            }
        }

    }

When im giving the username and password for the admin user it works. However when im trying to enter the username and passwod for FilmStudio I allways get the error messsage that says: "The username or password is incorrect."

Solution

[AllowAnonymous]
    [HttpPost("Authenticate")]
    public IActionResult Authenticate([FromBody] UserDto model)
    {
        if (model.UserName != null) // Check if UserName is null or not
        {
            var user = _userRepo.Authenticate(model.UserName, model.Password);
            if (user == null)
            {
                return BadRequest("The username or password is incorrect.");
            }

            return Ok(new
            {
                Id = user.UserId,
                Username = user.UserName,
                Role = user.Role,
                Token = user.Token
            });
        }
        else
        {
            var studio = _studioRepo.Authenticate(model.StudioName, model.StudioPassword);
            if (studio == null) 
            {
                return BadRequest("The username or password is incorrect.");
            }

            return Ok(new
            {
                Id = studio.StudioId,
                Username = studio.Name,
                City = studio.City,
                Role = studio.Role,
                Token = studio.Token
            });
        }
    }
}

Answered By – Joro

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *