// GET: Parents/ResetPassword // for parents
        public ActionResult ResetPassword(string id)
        {
            //Verify the reset password link
            //Find account associated with this link
            //redirect to reset password page
            if (string.IsNullOrWhiteSpace(id))
            {
                return(HttpNotFound());
            }

            using (ParentsRegsEntities parentsReg = new ParentsRegsEntities())
            {
                var user = parentsReg.ParentsLogins.Where(a => a.ResetPasswordCode == id).FirstOrDefault();
                if (user != null)
                {
                    ResetPassword reset = new ResetPassword();
                    reset.ResetCode = id;
                    return(View(reset));
                }
                else
                {
                    return(HttpNotFound());
                }
            }
        }
        public ActionResult ForgotPassword(string Email)
        {
            bool status = false;

            using (ParentsRegsEntities parentsReg = new ParentsRegsEntities())
            {
                var account = parentsReg.ParentsLogins.Where(a => a.Email == Email).FirstOrDefault();
                if (account != null)
                {
                    //Send email for reset password
                    string resetCode = Guid.NewGuid().ToString();
                    SendVerificationLinkEmail(account.Email, resetCode, "ResetPassword");
                    account.ResetPasswordCode = resetCode;

                    //Avoid confirm password not match issue, as we had added a confirm password property
                    parentsReg.Configuration.ValidateOnSaveEnabled = false;
                    parentsReg.SaveChanges();
                    ViewBag.Message = "Reset password link has been sent to your email id";
                    ModelState.Clear();
                }
                else
                {
                    ViewBag.ErrorMessage = "Sorry! account not found";
                }
            }
            return(View());
        }
 public bool IsEmailExist(string email)
 {
     using (ParentsRegsEntities parentsReg = new ParentsRegsEntities())
     {
         var v = parentsReg.ParentsLogins.Where(a => a.Email == email).FirstOrDefault();
         return(v != null);
     }
 }
        public ActionResult Register([Bind(Exclude = "IsEmailVerified,ActivationCode")] ParentsLogin parentsLogin)
        {
            bool   Status  = false;
            string message = "";

            if (ModelState.IsValid)
            {
                // Email is already Exist
                var isExist = IsEmailExist(parentsLogin.Email);
                if (isExist)
                {
                    ModelState.AddModelError("EmailExist", "Email already exist");
                    return(View(parentsLogin));
                }

                // Generate Activation Code
                parentsLogin.ActivationCode = Guid.NewGuid();

                // Password Hashing
                parentsLogin.Password        = Crypto.Hash(parentsLogin.Password);
                parentsLogin.ConfirmPassword = Crypto.Hash(parentsLogin.ConfirmPassword);

                parentsLogin.IsEmailVerified = false;

                // Save to Database
                using (ParentsRegsEntities parentsReg = new ParentsRegsEntities())
                {
                    Parents parents = new Parents();
                    parents.Id = parentsLogin.Id;
                    if (parents.checkId())
                    {
                        parentsReg.ParentsLogins.Add(parentsLogin);
                        parentsReg.SaveChanges();

                        // Send Email to User
                        SendVerificationLinkEmail(parentsLogin.Email, parentsLogin.ActivationCode.ToString());
                        message = "Registration successfully done. Account activation link " +
                                  " has been sent to your email id:" + parentsLogin.Email;
                        Status = true;
                    }
                    else
                    {
                        message = "Invalid id";
                    }
                }
            }
            else
            {
                message = "Invalid Request";
            }

            ViewBag.Message = message;
            ViewBag.Status  = Status;
            return(View());
        }
        public ActionResult Login(Parents parents, string ReturnUrl = "")
        {
            string message = "";

            using (ParentsRegsEntities parentsReg = new ParentsRegsEntities())
            {
                var v = parentsReg.ParentsLogins.Where(a => a.Id == parents.Id).FirstOrDefault();
                if (v != null)
                {
                    if (!v.IsEmailVerified)
                    {
                        ViewBag.ErrorMessage = "Please verify your email first";
                        return(View());
                    }
                    if (string.Compare(Crypto.Hash(parents.Password), v.Password) == 0)
                    {
                        int        timeout   = parents.RememberMe ? 1440 : 720; // 1440 min = 1 day && 720 min= 12 hour
                        var        ticket    = new FormsAuthenticationTicket(parents.Id, parents.RememberMe, timeout);
                        string     encrypted = FormsAuthentication.Encrypt(ticket);
                        HttpCookie cookie    = new HttpCookie("parents", encrypted);
                        cookie.Expires = DateTime.Now.AddMinutes(timeout);
                        Response.Cookies.Add(cookie);
                        ViewBag.User = Request.Cookies.Get("parents").Value;
                        if (Url.IsLocalUrl(ReturnUrl))
                        {
                            return(Redirect(ReturnUrl));
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Parents"));
                        }
                    }
                    else
                    {
                        message = "Invalid Id or password";
                    }
                }
                else
                {
                    message = "Invalid credential provided";
                }
            }
            ViewBag.ErrorMessage = message;
            return(View());
        }
        public ActionResult VerifyAccount(string id)
        {
            bool Status = false;

            using (ParentsRegsEntities parentsReg = new ParentsRegsEntities())
            {
                parentsReg.Configuration.ValidateOnSaveEnabled = false; // This line I have added here to avoid
                                                                        // Confirm password does not match issue on save changes
                var v = parentsReg.ParentsLogins.Where(a => a.ActivationCode == new Guid(id)).FirstOrDefault();
                if (v != null)
                {
                    v.IsEmailVerified = true;
                    parentsReg.SaveChanges();
                    Status = true;
                }
                else
                {
                    ViewBag.Message = "Invalid Request";
                }
            }
            ViewBag.Status = Status;
            return(View());
        }
 public ActionResult ResetPassword(ResetPassword reset)
 {
     if (ModelState.IsValid)
     {
         using (ParentsRegsEntities parentsReg = new ParentsRegsEntities())
         {
             var user = parentsReg.ParentsLogins.Where(a => a.ResetPasswordCode == reset.ResetCode).FirstOrDefault();
             if (user != null)
             {
                 user.Password          = Crypto.Hash(reset.NewPassword);
                 user.ResetPasswordCode = "";
                 parentsReg.Configuration.ValidateOnSaveEnabled = false;
                 parentsReg.SaveChanges();
                 ViewBag.message = "New password updated successfully";
             }
         }
         ModelState.Clear();
     }
     else
     {
         ViewBag.Errormessage = "Something invalid!";
     }
     return(View(reset));
 }