Ejemplo n.º 1
0
        public IActionResult Register(RegisterUserModel registerUser)
        {
            if (registerUser.username == null)
            {
                ModelState.Remove("username");
                ModelState.Remove("password");
                ModelState.Remove("confirmPassword");

                return(View("Register"));
            }
            else
            {
                //User Db context to register user
                if (UserDatabaseContext.CheckUsername(registerUser.username))
                {
                    UserDatabaseContext.Register(registerUser);
                    User user = new User();
                    user.username = registerUser.username;

                    user.accessToken = UserDatabaseContext.GenerateAccessToken(registerUser.username);

                    HttpContext.Session.SetString("user", JsonConvert.SerializeObject(user));
                }
                else
                {
                    TempData["err"] = "Username already exists";
                    return(View("Register"));
                }
            }

            UserPassModel userPass = new UserPassModel();

            userPass.username = registerUser.username;
            userPass.password = registerUser.password;

            return(RedirectToAction("Login", userPass));
        }
Ejemplo n.º 2
0
        public IActionResult Login(UserPassModel userPass)
        {
            //string userString = HttpContext.Session.GetString("user");
            if (userPass.username == null)
            {
                ModelState.Remove("username");
                ModelState.Remove("password");
                //auto sign in
                return(View("Login"));
            }
            else
            {
                if (ServerSideValidation.ValidateInfo(userPass))
                {
                    if (UserDatabaseContext.Login(userPass))
                    {
                        //Use Db context to validate user name and pass
                        TempData["User"] = userPass.username;

                        User user = new User();
                        user.username = userPass.username;

                        user.accessToken = UserDatabaseContext.GenerateAccessToken(userPass.username);

                        HttpContext.Session.SetString("user", JsonConvert.SerializeObject(user));

                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        TempData["err"] = "Invalid username or password";
                        return(View());
                    }
                }
                return(View());
            }
        }