Exemplo n.º 1
0
        public async Task <ActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = PortalDbContext.Get())
                {
                    if (!db.Users.Any(u => u.Email == model.Email))
                    {
                        db.Users.Add(new User()
                        {
                            Email        = model.Email,
                            PasswordHash = Hashing.GenerateHash(model.Password)
                        });
                        try
                        {
                            await db.SaveChangesAsync();

                            return(RedirectToAction("LogIn"));
                        }
                        catch (Exception)
                        {
                            ModelState.AddModelError("", "User Creation Unsuccessful");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Email", "An account for that email already exists");
                    }
                }
            }
            return(View(model));
        }
Exemplo n.º 2
0
        public ActionResult LogIn(LogInModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = PortalDbContext.Get())
                {
                    var user =
                        db.Users.FirstOrDefault(
                            u => u.Email.Equals(model.Email, StringComparison.InvariantCultureIgnoreCase));
                    if (user != null)
                    {
                        if (Hashing.CheckPassword(model.Password, user.PasswordHash))
                        {
                            FormsAuthentication.SetAuthCookie(user.Id.ToString(), model.RememberMe);
                            return(RedirectToAction("Edit"));
                        }
                    }
                }
            }

            return(View(model));
        }