Example #1
0
        public ActionResult Delete(string username)
        {
            DBModelEntities db   = new MvcAdminTemplate.Models.DBModelEntities();
            var             user = db.Accounts.FirstOrDefault(u => u.Username == username);

            if (user != null)
            {
                db.Accounts.Remove(user);
                db.SaveChanges();
                return(RedirectToAction("Delete", "Account"));
            }

            else
            {
                ModelState.AddModelError("", "User does not exist");
                return(View());
            }
        }
Example #2
0
        private bool IsValid(string username, string password)
        {
            var  crypto  = new SimpleCrypto.PBKDF2();
            bool IsValid = false;

            using (DBModelEntities db = new MvcAdminTemplate.Models.DBModelEntities())
            {
                var user = db.Accounts.FirstOrDefault(u => u.Username == username);
                if (user != null)
                {
                    if (user.Password == crypto.Compute(password, user.PasswordSalt))
                    {
                        IsValid = true;
                    }
                }
            }
            return(IsValid);
        }
Example #3
0
        public ActionResult Reset(string username, string password1, string password2)
        {
            DBModelEntities db   = new MvcAdminTemplate.Models.DBModelEntities();
            var             user = db.Accounts.FirstOrDefault(u => u.Username == username);

            if (user != null && password1 == password2)
            {
                var crypto     = new SimpleCrypto.PBKDF2();
                var hashedPass = crypto.Compute(password1); // Hashes user password
                user.Password     = hashedPass;
                user.PasswordSalt = crypto.Salt;
                db.SaveChanges();
                return(RedirectToAction("Reset", "Account"));
            }

            else
            {
                ModelState.AddModelError("", "Data is incorrect");
                return(View());
            }
        }
Example #4
0
        public ActionResult Register(Models.Account user, string passwordconfirm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (db.Accounts.Where(u => u.Username == user.Username).Any())
                    {
                        ModelState.AddModelError("Username", "Username is already taken");
                        return(View());
                    }

                    else
                    {
                        using (DBModelEntities db = new MvcAdminTemplate.Models.DBModelEntities())
                        {
                            if (user.Password == passwordconfirm)
                            {
                                // Hashes using SimpleCrypto Lib
                                // Will be changed to Argon2 in the future
                                var crypto     = new SimpleCrypto.PBKDF2();
                                var hashedPass = crypto.Compute(user.Password); // Hashes user password
                                var newUser    = db.Accounts.Create();
                                newUser.Username     = user.Username;
                                newUser.Password     = hashedPass;
                                newUser.PasswordSalt = crypto.Salt;
                                newUser.OrgID        = 10; // hardcoded for now (should be user.Organization)
                                newUser.First        = user.First;
                                newUser.Last         = user.Last;
                                newUser.Role         = "User";
                                newUser.CreatedOn    = DateTime.Now;
                                db.Accounts.Add(newUser);
                                db.SaveChanges();
                                return(RedirectToAction("Register", "Account"));
                            }
                            else
                            {
                                ModelState.AddModelError("Password", "Passwords must match");
                                return(View());
                            }
                        }
                    }
                }

                else
                {
                    ModelState.AddModelError("", "Data is incorrect");
                }
            }

            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);

                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }

                throw;
            }

            return(View());
        }