Esempio n. 1
0
        public ActionResult Authorize(LoginModel loginUser)
        {
            UserModel u = new UserModel();

            u.email    = (string)loginUser.email;
            u.password = (string)loginUser.password;
            UserModel user = SqliteDataAccess.AuthorizeUser(u);



            if (ModelState.IsValid && user != null)
            {
                //creates hash of inserted password with the salt that belongs to the username
                passwordEncryptionController hashPW = new passwordEncryptionController();
                hashPW.setSalt(user.salt);
                var hash = hashPW.generateHash(u.password, true);

                Console.WriteLine("salt: " + user.salt);
                Console.WriteLine("hash: " + hash);
                Console.WriteLine("password: "******"Authenticated!");
                    HttpContext.Session.SetString("UserSession", user.role);
                    HttpContext.Session.SetString("UserName", user.fname);
                    HttpContext.Session.SetInt32("UserClub", user.club);
                    HttpContext.Session.SetInt32("UserID", user.userID);

                    return(RedirectToAction("Dashboard", "Home"));
                }
                else
                {
                    Console.WriteLine("Access Denied!");
                    return(RedirectToAction("Login", "Home"));
                }
            }
            else
            {
                Console.WriteLine("Access Denied!");
                return(RedirectToAction("Login", "Home"));
            }
        }
Esempio n. 2
0
        public ActionResult Register(RegisterModel user)
        {
            UserModel newUser = new UserModel();

            newUser.fname     = user.FnameField;
            newUser.lname     = user.LnameField;
            newUser.email     = user.EmailField;
            newUser.password  = user.PasswordField;
            newUser.birthdate = user.BirthField;
            newUser.gender    = user.GenderField;
            newUser.role      = user.RoleField;


            UserModel replica = SqliteDataAccess.UserExist(newUser);

            if (replica == null)
            {
                Console.WriteLine("replica: " + replica);
                if (ModelState.IsValid)
                {
                    //Creates encryption object and adds hash + salt to users password and salt
                    passwordEncryptionController newPW = new passwordEncryptionController();
                    newPW.generateSalt();
                    var hash = newPW.generateHash(newUser.password, true);
                    var salt = newPW.getSalt();

                    newUser.password = hash;
                    newUser.salt     = salt;
                    Console.WriteLine("hash: " + hash);
                    Console.WriteLine("salt: " + salt);


                    // Saves User in user table
                    SqliteDataAccess.SavePerson(newUser);

                    // inserts role application
                    SqliteDataAccess.RoleApplication(newUser);
                    //first time login sets session
                    HttpContext.Session.SetString("UserSession", "default");
                    HttpContext.Session.SetString("UserName", newUser.fname);


                    return(RedirectToAction("Dashboard", "Home"));
                }
                else
                {
                    return(RedirectToAction("Register", "Home"));
                }
            }
            else if (replica.fname == "birthdate duplicate")
            {
                Console.WriteLine("replica: " + replica.fname);
                return(RedirectToAction("Register", "Home"));
            }
            else if (replica.fname == "email duplicate")
            {
                Console.WriteLine("replica: " + replica.fname);
                return(RedirectToAction("Register", "Home"));
            }
            else
            {
                Console.WriteLine("replica: " + replica.fname);
                return(RedirectToAction("Register", "Home"));
            }
        }