public async Task <ActionResult> Register(AllUserInfo input, [FromServices] UserManager <IdentityUser> userManager, [FromServices] RoleManager <IdentityRole> roleManager) { // with an [ApiController], model state is always automatically checked // and return 400 if any errors. bool admin = input.IsAdmin; bool UsernameTaken = Repo.CheckIfUsernameInDatabase(input.Username); if (UsernameTaken) { return(StatusCode(418, UsernameTaken)); } log.Info("Beginning new user registration"); var user = new IdentityUser(input.Username); var result = await userManager.CreateAsync(user, input.Password); if (!result.Succeeded) { log.Info("HTTP status code 400 - displaying error view"); return(BadRequest(result)); } log.Info("HTTP status code 200 - continuing with login"); if (admin) { log.Info("User is adminiatrator"); if (!(await roleManager.RoleExistsAsync("admin"))) { log.Info("Creating admin role"); var adminRole = new IdentityRole("admin"); result = await roleManager.CreateAsync(adminRole); if (!result.Succeeded) { log.Info("Error: internal server error. Displaying result"); return(StatusCode(500, result)); } } log.Info("Administration role exists"); log.Info("Adding admin role to user"); result = await userManager.AddToRoleAsync(user, "admin"); if (!result.Succeeded) { log.Info("Error: internal server error. Displaying result"); return(StatusCode(500, result)); } } log.Info("Logging in user"); await _signInManager.SignInAsync(user, isPersistent : false); log.Info("Creating user for non-identity database"); var permission = (input.IsAdmin); Users u = new Users { Username = input.Username, FirstName = input.FirstName, LastName = input.LastName, DateOfBirth = input.DateOfBirth, UserAddress = input.UserAddress, PhoneNumber = input.PhoneNumber, Email = input.Email, LevelPermission = permission, UserPic = input.UserPic }; await Repo.AddUserAsync(u); log.Info("User registration successful"); return(NoContent()); }