public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (!CaptchaHelper.Validate())
            {
                ModelState.AddModelError("", "Invalid Captcha");
                return(View(model));
            }

            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByEmailAsync(model.Email);

                if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
                {
                    // Don't reveal that the user does not exist or is not confirmed
                    return(RedirectToAction(nameof(ForgotPasswordConfirmation)));
                }

                string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

                var callbackUrl = Url.Action(nameof(ResetPassword), "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                await EmailService.SendEmail(Enums.EmailTemplateType.PasswordReset, user.Id, user.Email, user.UserName, callbackUrl);

                return(RedirectToAction(nameof(ForgotPasswordConfirmation)));
            }

            return(View(model));
        }
        public async Task <ActionResult> ResetPassword(ResetPasswordViewModel model)
        {
            if (!CaptchaHelper.Validate())
            {
                ModelState.AddModelError("", "Invalid Captcha");
                return(View(model));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await UserManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                // Don't reveal that the user does not exist
                return(RedirectToAction(nameof(ResetPasswordConfirmation)));
            }

            var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);

            if (result.Succeeded)
            {
                return(RedirectToAction(nameof(ResetPasswordConfirmation)));
            }
            AddErrors(result);
            return(View());
        }
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!CaptchaHelper.Validate())
            {
                ModelState.AddModelError("", "Invalid Captcha");
                return(View(model));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // Check User
            var user = await UserManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                ModelState.AddModelError("", "Incorrect Email or Password");
                return(View(model));
            }

            if (!await UserManager.IsEmailConfirmedAsync(user.Id))
            {
                ModelState.AddModelError("", "Email not activated, please check your email");
                return(View(model));
            }

            if (await UserManager.IsLockedOutAsync(user.Id))
            {
                ModelState.AddModelError("", "Account locked");
                return(View(model));
            }

            if (!await UserManager.CheckPasswordAsync(user, model.Password))
            {
                await IncrementAccessFailedCount(user, "Incorrect Email or Password");

                return(View(model));
            }


            // Reset failed attempts
            await UserManager.ResetAccessFailedCountAsync(user.Id);

            await SignInAsync(user);

            if (string.IsNullOrEmpty(returnUrl))
            {
                return(RedirectToAction("Index", "Home"));
            }
            return(RedirectToLocal(returnUrl));
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (!CaptchaHelper.Validate())
            {
                ModelState.AddModelError("", "Invalid Captcha");
                return(View(model));
            }

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName       = model.UserName,
                    Email          = model.Email,
                    EmailConfirmed = false,
                    Points         = 0
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await AwardWriter.AddUserAward(new AddUserAwardModel
                    {
                        UserId = user.Id,
                        Type   = AwardType.Registration,
                    });

                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action(nameof(ConfirmEmailAddress), "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    await EmailService.SendEmail(Enums.EmailTemplateType.Registration, user.Id, user.Email, user.UserName, callbackUrl);

                    return(RedirectToAction(nameof(ConfirmEmail)));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }