示例#1
0
        public async Task <IActionResult> ForgetPassword(ForgetPasswordViewModel model)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Profile"));
            }

            UserModel user = await ManageUser.FindByEmailAsync(model.Email);

            ViewBag.Sent = false;
            if (user != null)
            {
                var token = await ManageUser.GeneratePasswordResetTokenAsync(user);

                var callbackUrl = Url.Action("ResetPassword", "Account", new { token = token, email = user.Email });
                var url         = "https://" + Request.Host + callbackUrl;

                MailRequest mailrequest = new MailRequest
                {
                    ToEmail = model.Email,
                    Subject = "Password Reset Token",
                    Body    = "<div style='text-align:center'> <h1 style='margin:20px;'> Reset Your Password. </h1> <a href='" + url + "' > Reset Password </a> </div>"
                };

                var result = await SendToken(mailrequest);

                if (result)
                {
                    ViewBag.Sent = true;
                }
            }

            return(View());
        }
示例#2
0
        public async Task <IActionResult> Login(string returnUrl, LoginViewModel model)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Profile"));
            }

            if (ModelState.IsValid)
            {
                UserModel user = await ManageUser.FindByEmailAsync(model.Email);

                if (user != null && await ManageUser.CheckPasswordAsync(user, model.Password))
                {
                    if (await ManageUser.IsEmailConfirmedAsync(user))
                    {
                        var result = await SignManager.PasswordSignInAsync(user, model.Password, true, false);

                        if (result.Succeeded)
                        {
                            if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
                            {
                                return(LocalRedirect(returnUrl));
                            }
                            else
                            {
                                return(RedirectToAction("Index", "Home"));
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("Error", result.ToString());
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Error", "Email is not verified.");
                    }
                }
                else
                {
                    ModelState.AddModelError("Error", "Failed : Invalid Login Attempt");
                }
            }

            return(View(model));
        }
示例#3
0
        // GET: Account/ConfirmEmail?token=value&email=value
        public async Task <IActionResult> ConfirmEmail(string token, string email)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Profile"));
            }

            UserModel user = await ManageUser.FindByEmailAsync(email);

            if (user != null && !await ManageUser.IsEmailConfirmedAsync(user))
            {
                var result = await ManageUser.ConfirmEmailAsync(user, token);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Login"));
                }
            }

            return(View("Verify"));
        }
示例#4
0
        public async Task <IActionResult> ResetPassword(ResetPasswordViewModel model)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Profile"));
            }

            UserModel user = await ManageUser.FindByEmailAsync(model.Email);

            if (user != null)
            {
                var passwordValidator = new PasswordValidator <UserModel>();
                var result            = await passwordValidator.ValidateAsync(ManageUser, null, model.Password);

                if (result.Succeeded)
                {
                    result = await ManageUser.ResetPasswordAsync(user, model.Token, model.Password);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Login"));
                    }
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.TryAddModelError(error.Code, error.Description);
                    }

                    ViewBag.token = model.Token;
                    ViewBag.email = model.Email;

                    return(View("ResetPassword"));
                }
            }

            return(View("Verify"));
        }