Exemplo n.º 1
0
        /* This method is used for authentication in login and check if there
         * exist user in the base */
        private bool isValid(string username, string password, bool forExist)
        {
            var  crypto     = new SimpleCrypto.PBKDF2();
            bool validation = false;

            using (var db = new Blog.Models.BlogDataEntities())
            {
                var user = db.User.FirstOrDefault(u => u.Username == username);

                if (user != null)
                {
                    if (forExist == true)
                    {
                        return(true);
                    }

                    if (user.Password == crypto.Compute(password, user.PasswordSalt))
                    {
                        validation = true;
                    }
                }
            }

            return(validation);
        }
Exemplo n.º 2
0
        public ActionResult Register(Models.RegisterUser user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (var db = new Blog.Models.BlogDataEntities())
                    {
                        var crypto      = new SimpleCrypto.PBKDF2();
                        var encryptPass = crypto.Compute(user.Password);

                        var newUser = db.User.Create();

                        newUser.Username     = user.Username;
                        newUser.Password     = encryptPass;
                        newUser.PasswordSalt = crypto.Salt;
                        newUser.FirstName    = user.FirstName;
                        newUser.LastName     = user.LastName;
                        newUser.DateCreated  = DateTime.Now;

                        if (isValid(newUser.Username, newUser.Password, true))
                        {
                            ModelState.AddModelError("", "The username exist in the database. Please choice another username");
                            return(View());
                        }

                        db.User.Add(newUser);
                        db.SaveChanges();

                        TempData["successRegister"] = "Successful register";
                        return(RedirectToAction("Login", "User"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Data is not correct!");
                }
            }
            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());
        }