0

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."

1
  • You have to post UserDTo, StudioDto, _studioRepo.Authenticate and view where you submit the name and password Commented Feb 5, 2022 at 20:08

2 Answers 2

1
[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
            });
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can try to use instead of model.Name model.UserName for studio too

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

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

and IMHO you can fix user part too


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

2 Comments

The property for Studio is Name though
@MarcusRosberg " im trying to enter the username and passwod for FilmStudio " you have to fix the question then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.