Exemple #1
0
        public ActionResult Register(Models.Registration user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (var context = new LoginInMVC4WithEF.Models.CMPSEntities6())
                    {
                        var crypto     = new SimpleCrypto.PBKDF2();
                        var encrypPass = crypto.Compute(user.Password);
                        var newUser    = context.Registrations.Create();
                        newUser.UserId       = user.UserId;
                        newUser.Email        = user.Email;
                        newUser.Password     = encrypPass;
                        newUser.Passwordsalt = crypto.Salt;
                        newUser.FirstName    = user.FirstName;
                        newUser.LastName     = user.LastName;
                        newUser.IsActive     = true;
                        context.Registrations.Add(newUser);
                        context.SaveChanges();

                        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());
        }
Exemple #2
0
        private bool IsValid(string email, string password)
        {
            var  crypto  = new SimpleCrypto.PBKDF2();
            bool IsValid = false;

            using (var db = new LoginInMVC4WithEF.Models.CMPSEntities6())
            {
                var user = db.Registrations.FirstOrDefault(u => u.Email == email);
                if (user != null)
                {
                    if (user.Password == crypto.Compute(password, user.Passwordsalt))
                    {
                        IsValid = true;
                    }
                }
            }
            return(IsValid);
        }