public async Task<ActionResult> Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var account = repository
                .FindBy(a => a.Email == model.Email)
                .FirstOrDefault();

            if (account != null)
            {
                ModelState.AddModelError("Email", "An account with this email address already exists.");
                return View(model);
            }

            account = new Account();
            account.Email = model.Email;
            account.Name = model.Name;
            account.PasswordHashed = PasswordHasher.HashPassword(model.Password);
            account.IsActive = true;

            repository.Add(account);
            repository.Save();

            var claims = new List<Claim>();
            claims.Add(new Claim(ClaimTypes.Email, account.Email));
            
            await Context.Authentication.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme, 
                new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme)), 
                new AuthenticationProperties() { IsPersistent = true }
            );

            return RedirectToAction("Index", "Home");
        }
Exemplo n.º 2
0
        public ActionResult Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var account = session.QueryOver<Account>()
                    .Where(a => a.Email == model.Email)
                    .List().FirstOrDefault();

            if (account != null)
            {
                ModelState.AddModelError("Email", "An account with this email address already exists.");
                return View(model);
            }

            account = new Account();
            account.Email = model.Email;
            account.Name = model.Name;
            account.PasswordHashed = PasswordHasher.HashPassword(model.Password);
            account.IsActive = true;

            session.Save(account);

            FormsAuthentication.SetAuthCookie(account.Email, true);

            return RedirectToAction("Index", "Home");
        }