skip to Main Content

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

2

Answers


  1. 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
        });
    
    Login or Signup to reply.
  2. [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
                });
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search