public void Delete(User entity)
 {
     if (context.Entry(entity).State == EntityState.Detached)
         {
             dbSet.Attach(entity);
         }
         dbSet.Remove(entity);
 }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // Появление этого сообщения означает наличие ошибки; повторное отображение формы
            return View(model);
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var confirmationToken = CreateConfirmationToken();
                var user = new User() { UserName = model.UserName, Email = model.Email, DateOfBirth = model.DateOfBirth, ConfirmationToken = confirmationToken, IsConfirmed = false };
                var result = await UserManager.CreateAsync(user, model.Password);
                
                if (result.Succeeded)
                {
                    SendEmail(model.Email, model.UserName, user.ConfirmationToken);
                    //await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // Появление этого сообщения означает наличие ошибки; повторное отображение формы
            return View(model);
        }
 private async Task SignInAsync(User user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
 }
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Получение сведений о пользователе от внешнего поставщика входа
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new User() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
 public void Update(User entity)
 {
     dbSet.Attach(entity);
         context.Entry(entity).State = EntityState.Modified;
 }
 public void Create(User entity)
 {
     dbSet.Add(entity);
 }
        public async Task<ActionResult> Register(RegisterViewModel model, HttpPostedFileBase avatar)
        {
            try
            {
                if (ModelState.IsValid && avatar != null && CheckExtension(Path.GetExtension(avatar.FileName).ToLower()))
                {
                    var confirmationToken = CreateConfirmationToken();
                    var user = new User() { UserName = model.UserName, Email = model.Email, DateOfBirth = model.DateOfBirth, ConfirmationToken = confirmationToken, IsConfirmed = false, Avatar = SaveAvatar(avatar, model.UserName) };
                    var result = await UserManager.CreateAsync(user, model.Password);
                    if (result.Succeeded)
                    {
                        SendEmail(model.Email, model.UserName, user.ConfirmationToken);
                        //await SignInAsync(user, isPersistent: false);
                        return View("ConfirmationRequest");
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }

                // Появление этого сообщения означает наличие ошибки; повторное отображение формы
                return View(model);
            }
            catch (Exception exception)
            {
                return RedirectToAction("Error", "Home", new {errorMessage = exception.Message});
            }
        }