コード例 #1
0
        public ActionResult Index(Credentials credModel)
        {
            string usernameC = credModel.username;
            string passwordC = credModel.password;

            var credentialsDbManager  = new CredentialsManager(Configuration);
            int idUserTryingToConnect = credentialsDbManager.GetIdCredentials(usernameC);
            int userStatus            = credentialsDbManager.GetStatus(usernameC);

            //Get password using
            if (passwordC == credentialsDbManager.GetPassword(usernameC))
            {
                HttpContext.Session.SetString("username", usernameC);
                HttpContext.Session.SetInt32("id", idUserTryingToConnect);
                HttpContext.Session.SetInt32("userType", userStatus);
                //status 2 means admin, 1=Delivery employee, 0 = customer
                //The view the user will see depends on his status in our DB
                if (userStatus == 2)
                {
                    return(RedirectToAction("Index", "Home"));
                }

                else if (userStatus == 1)
                {
                    StaffManager sManager = new StaffManager(Configuration);
                    int          idStaff  = sManager.GetStaffId(idUserTryingToConnect);
                    HttpContext.Session.SetInt32("idStaff", idStaff);
                    return(RedirectToAction("Index", "DishesOrder"));
                }
                else if (userStatus == 0)
                {
                    CustomerManager cManager = new CustomerManager(Configuration);
                    HttpContext.Session.SetInt32("idCustomer", cManager.GetCustomerIDByCredentials(idUserTryingToConnect));
                    return(RedirectToAction("Index", "Home"));
                }
                //If we did find a username and password but not a status we through an error.
                return(RedirectToAction("LoginError", "Error", new { message = "Your account is not correctly initialized. Please contact our support : [email protected] or connect with another account." }));
            }
            else
            {   //If the credentials did not match we through an error
                return(RedirectToAction("LoginError", "Error", new { message = "Unfortunately your username or password did not match our records. Please try again." }));
            }
        }